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

alfaiz678

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



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


These codes are used below
B4X:
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

B4X:
 <?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());

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

aeric

Expert
Licensed User
Longtime User
You have too many errors.

Do you mean
B4X:
$conn = mysql_connect($servername, $username, $password) or die(mysql_error());

and
B4X:
include('conn.php');

Also mysql_connect is deprecated.
 
Upvote 0

alfaiz678

Active Member
Licensed User
B4X:
 <?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());

?>

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


You edited and no use
 
Upvote 0

aeric

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



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


These codes are used below
B4X:
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

B4X:
 <?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());

?>
B4X:
<?php
incliude ('conn.php');
$num = strip_tags(trim($_post["k"]));
mysql_query("INSERT INTO kkkk (k) VALUES ('$num')");
 ?>
It seems you have messed up with $num and k key.
 
Upvote 0

alfaiz678

Active Member
Licensed User
You need to enter the value for server name such as localhost, username and password for the database.
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
 
Upvote 0

alfaiz678

Active 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

B4X:
<?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();
 ?>

B4X:
 <?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";


?>

B4X:
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:
Upvote 0

alfaiz678

Active Member
Licensed User
Tips:
1. You are not calling Job.Release.
2. Simpler to use Wait For: https://www.b4x.com/android/forum/threads/79345/#content


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:
B4X:
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

B4X:
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

B4X:
Sub Button2_Click
    jop11.Initialize("show",Me)
    jop11.Download("http://www.mysite.com/dbphp/select.php")
End Sub


php
B4X:
<?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:

B4X:
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 1: syntax error

At this line
B4X:
    parser.Parse(istream,"parser_import")
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

alfaiz678

Active 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.
.
I will try what I mentioned in the five notes


.
6. I'm not sure why you are using this PHP at all. Better use jRDC2.
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 ???
 
Upvote 0

alfaiz678

Active Member
Licensed User
You do need a VPS for this. However the price of VPS today is similar to shared hosting.

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?
 
Upvote 0
Top