Android Question I tried connecting to mySQL database with b4a and it gets an error

Discussion in 'Android Questions' started by alfaiz678, Aug 23, 2019.

  1. alfaiz678

    alfaiz678 Member Licensed User

    I tried connecting to MySQL database with b4a
    Use the OkHttpUtils2 library
    This error appears
    Code:
    ResponseError. Reason: Internal Server Error, Response:


    When I use the HttpUtils2 library
    This error appears
    Code:
    (IntentIntent { cmp=b4a.exampleasd/anywheresoftware.b4a.samples.httputils2.httputils2service }

    These codes are used below
    Code:
    Sub Button1_Click
        jop1.Initialize(
    "at",Me)
        jop1.PostString(
    "http://www.mysite.com/insert.php","$num="&EditText1.Text)
    End Sub


    Sub JobDone(Job As HttpJob)
    If Job.Success Then
        
    If Job.JobName ="at" Then
            
    ToastMessageShow("yes",True)
            
    Else
                
    ToastMessageShow("no",True)
        
    End If
    End If
    Code:
    <?php
    $servername = 
    "";
    $username = 
    "";
    $password = 
    "";
    $dbnamee = 
    "";

    // Create connection
    $conn = mysql_connect($servername,$dbnamee, $username, $password) 
    or di(mysql_error());
    mysql_select_db($dbnamee) 
    or di(mysql_error());

    ?>
    Code:
    <?php
    incliude (
    'conn.php');
    $num = strip_tags(trim($_post["k"]));
    mysql_query(
    "INSERT INTO kkkk (k) VALUES ('$num')");
     ?>
     
  2. aeric

    aeric Active Member Licensed User

    You have too many errors.

    Do you mean
    Code:
    $conn = mysql_connect($servername, $username, $password) or die(mysql_error());
    and
    Code:
    include('conn.php');
    Also mysql_connect is deprecated.
     
  3. alfaiz678

    alfaiz678 Member Licensed User

    Code:
    <?php
    $servername = 
    "";
    $username = 
    "";
    $password = 
    "";
    $dbnamee = 
    "";

    // Create connection
    $conn = mysql_connect($servername, $username, $password) 
    or die (mysql_error());
    @mysql_select_db($dbnamee) 
    or die (mysql_error());

    ?>
    Code:
    <?php
    include (
    "conn.php");
    $num = strip_tags(trim($_post[
    "k"]));
    mysql_query(
    "INSERT INTO kkkk(k) VALUES('$num')");
     ?>

    You edited and no use
     
  4. aeric

    aeric Active Member Licensed User

    You need to enter the value for server name such as localhost, username and password for the database.
     
  5. aeric

    aeric Active Member Licensed User

    Try run using web browser first before using it in b4a.
     
    f0raster0 likes this.
  6. aeric

    aeric Active Member Licensed User

    It seems you have messed up with $num and k key.
     
  7. alfaiz678

    alfaiz678 Member Licensed User

    You have entered all the information and it is correct
    Host Name
    Name base
    user name
    Password

    Also created a table named KKKK
    It has one text column named k
    To experience
     
  8. alfaiz678

    alfaiz678 Member Licensed User

    Can you tell me in a way that
     
  9. alfaiz678

    alfaiz678 Member Licensed User

    The problem was finally resolved
    The reason for the different codes
    PHP
    Modern version 5 and above
    The corrected code has been placed below for the benefit

    Code:
    <?php
    include (
    "conn.php");
    $num = strip_tags($_POST[
    "k"]);

    $
    sql="INSERT INTO kkkk(k) VALUES('$num')";


    if ($conn->query($sql) === TRUE) {
        echo 
    "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
     ?>
    Code:
    <?php
    $servername = 
    "";
    $username = 
    "";
    $password = 
    "";
    $dbnamee = 
    "";

    // Create connection

        $conn = new mysqli($servername, $username, $password, $dbnamee);
      //  $conn = mysql_connect($servername, $username, $password) 
    or die (mysql_error());
    //@mysql_select_db($dbnamee) 
    or die (mysql_error());



    if ($conn->connect_error) {
        die(
    "Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";


    ?>
    Code:
    Sub Button1_Click
        jop1.Initialize(
    "at",Me)
        jop1.PostString(
    "http://www.mysite.com/insert.php","&k="&EditText1.Text)
     
    End Sub


    Sub JobDone(Job As HttpJob)
        
    If Job.Success Then
            
    If Job.JobName ="at" Then
                
    Log("yessssssssssssssss")
            
    Else
                
    Log("noooooooooooooooo")
            
    End If
        
    End If


    Thanks aeric for the interaction
     
    Last edited: Aug 24, 2019
  10. aeric

    aeric Active Member Licensed User

    For first parameter, you don't need add & in front. You only add & when you have more than one parameter.

    Check: HttpUtils2 - Web services are now even simpler
     
    mangojack and José J. Aguilar like this.
  11. alfaiz678

    alfaiz678 Member Licensed User

    Thanks for the information
    Thanks
     
  12. Erel

    Erel Administrator Staff Member Licensed User

  13. alfaiz678

    alfaiz678 Member Licensed User


    Thanks Earl
    I would also like to complete the implementation of what I started with
    You have successfully entered data


    Data Display Process:
    You have applied this code:
    Code:
    Sub JobDone(Job As HttpJob)

        
    If Job.Success Then
            
    If Job.JobName ="show" Then
                
    Dim istream As InputStream
                istream=Job.GetInputStream
                parser.Initialize
                parser.Parse(istream,
    "parser_import")
                istream.Close
               
    '            Log("yessssssssssssssss")
    '        Else
    '            Log("noooooooooooooooo")
            End If
        
    End If
       
       
       
    End Sub
    Code:
    Sub parser_import_EndElement (Uri As String, Name As String, Text As StringBuilder)
        
    If parser.Parents.IndexOf("item")>-1 Then
            
    If Name="num" Then
            num1=Text.ToString  
            
    End If
        
    End If
       
        
    If Name="item" Then
            ListView1.AddSingleLine(num1)
        
    End If
    End Sub
    Code:
    Sub Button2_Click
        jop11.Initialize(
    "show",Me)
        jop11.Download(
    "http://www.mysite.com/dbphp/select.php")
    End Sub

    php
    Code:
    <?php
    include (
    "config.php");
    echo 
    '<?xml version="1.0" encoding="UTF-8"?>';

     ?>
     <rss version=
    "2.0">
         <channel>
             <?php>
             $
    sql = "select k from asd";
       
    $result = $conn->query($
    sql);

    if ($result->num_rows > 0) {
        // output data of 
    each row
        
    while($row = $result->fetch_assoc()) {
            ?>
    <item>
        <num><<?php echo $row [
    'k']; ?><num>
    </item>
    <<?php 
    } ?>
    <item2>
        <end> <<?php echo "end"; ?></end>
    </item2>

    </channel>
    </rss>
    I have run PHP code on my browser and it works fine
    And when you connect it to Basic 4 Android
    This error appears:

    Code:
    org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 1: syntax error
    At this line
    Code:
    parser.Parse(istream,"parser_import")
     
  14. alfaiz678

    alfaiz678 Member Licensed User

  15. Erel

    Erel Administrator Staff Member Licensed User

    1. You are still using JobDone although it is not needed.
    2. You haven't posted the full error message.
    3. You can use Xml2Map to parse the XML string.
    4. Based on the error message the XML is invalid.
    5. Add Log(Job.GetString) and see what you are receiving.
    6. I'm not sure why you are using this PHP at all. Better use jRDC2.
     
  16. alfaiz678

    alfaiz678 Member Licensed User

    I will try what I mentioned in the five notes


    The last note
    I hope to work on jRDC2, but it needs a local server machine to be the intermediary between the application and the remote server on the Internet
    I use PHP for this reason only because the broker, PHP does not need a local server, it is on the web server
    Can you have jRDC2 running on internet server only ???
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Yes!!! This is the recommended way to run jRDC2.

    You do need a VPS for this. However the price of VPS today is similar to shared hosting.
     
  18. alfaiz678

    alfaiz678 Member Licensed User

    You searched Google
    About VPS hosting prices
    I found their prices as I mentioned an approach to shared hosting prices

    Is there another way that Erel recommends?
     
  19. f0raster0

    f0raster0 Active Member Licensed User

    share here your full project, all your files: B4A, php and MySQL, it will be easer to help.
     
  20. alfaiz678

    alfaiz678 Member Licensed User

    Thank you so much
    I'll do it
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice