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')");
 ?>
 

alfaiz678

Active Member
Licensed User
share here your full project, all your files: B4A, php and MySQL, it will be easer to help.

This is the project as per your request
Here's a question is it possible to upload the config.php file
Only on hosting
The rest of the commands such as add modification and deletion and .....
Via the app directly
 

Attachments

  • 11.zip
    152.3 KB · Views: 161
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is the project as per your request
there are multiple problems in your php script (i only checked insert.php)

1. Which php version are you running??? I ask because mysql_query is deprecated since years.
2. jop1.PostString do exactly this. the payload is posted to the phpscript. The input can be get via
PHP:
<?php $postdata = file_get_contents("php://input"); ?>
$postdata contains the payload. Note that you need to manually extract the values from the payload as the code above does not parse anything.

Better is to use job.PostMultipart
3. The B4A code misses a DIM for each job you are sending. This is a mistake.
B4X:
Sub Button1_Click
    Dim j As HttpJob
    j.Initialize("at",Me)
    j.PostMultipart("http://www.mysite.com/insert.php",CreateMap("k": EditText1.Text.Trim),Null)
End Sub
in this case php is doing the parsing for you and deliver the value of k in $_POST["k"]

4. Set errorreporting in php so that you see any error in the output if there is an error.
Place this line at the 1. line of the php-scripts
PHP:
error_reporting(E_ALL & ~E_NOTICE);
 
Last edited:
Upvote 0

alfaiz678

Active Member
Licensed User
The problem is that I only speak a little PHP
So you can correct the code thanks


Here's a question is it possible to upload the config.php file
Only on hosting
The rest of the commands such as add modification and deletion and .....
Via the app directly

You could answer that question ؟؟
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you are not familar with php then i suggest to
1. Switch to jRDC2 (you need a VPS). Best and safest Solution
2. Learn PHP or hire someone who write the php-code for you
3. Use jdbcsql. This only works if your Database is accessible from outside. Not recommended for production apps!
 
Upvote 0

alfaiz678

Active Member
Licensed User
thanks for the information
Even though the insert was successful with me (insert)
The problem with me was in the view (select)
And you did
Tweaking the part that works

I will try again
 
Upvote 0
Top