Android Question Put data to MySQL Database?

ilan

Expert
Licensed User
Hi

until today i have sending data to my mySQL DB via php. I have a shared hosting and some mysql db on it. is there a different way to send data to mysql?

it seems like when i send it via php it is not updated immidiatley. sometimes i take some minutes until i see the updated data. is it normal like this? can it be that it has something to do with okhttp?

thanx, ilan
ps.: my hosting is on WHOIS.COM. if you know a better company please let me know i am ready to pay more for my hosting if it runs faster and better. thanx
 

DonManfred

Expert
Licensed User
it seems like when i send it via php it is not updated immidiatley. sometimes i take some minutes until i see the updated data. is it normal like this?
no. When i call a php and in the php i write data to the db the data is written immediately. A following request to get data get the updated data from db without problem.

can it be that it has something to do with okhttp?
i don´t think so.

Start with posting all relevant code.
 

José J. Aguilar

Well-Known Member
Licensed User
I use jRDC2 and I can see the changes immediately. But you should use a VPS
I can't tell you for sure if it's normal through php, but I guess that's not the normal behaviour.
 

ilan

Expert
Licensed User
thanx for all answers.

Start with posting all relevant code.
this is my phpcode:

B4X:
<?

$table = "";

include ("");
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$action = $_GET["action"];
 
switch ($action)

{

    Case "GetUserName":
        $username = $_GET["username"];
        $q = mysql_query("SELECT password FROM $table WHERE username='$username'");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
    
    case "SignUp":
        $username = $_GET["username"];
        $password = $_GET["password"];
        $q = mysql_query("INSERT INTO $table (username, password) VALUES ('$username', '$password')");
        print "Inserted";
    break;
    
    
}


?>
b4a code:

B4X:
Dim GetPersons As HttpJob
        GetPersons.Initialize("GetUserN", Me)
        GetPersons.download2(domain & "8bitlogin.php", Array As String ("action", "GetUserName", "username", user))
if JobDone event i am getting the data and showing to the user...

assuming that the server is configured to allow remote access).
remote.jpg


is this what you mean erel?
 

José J. Aguilar

Well-Known Member
Licensed User
I think he means that you probably have your php pages in the same server that your mysql, so your php files connect to localhost.

Try to connect to your database from your computer with i.e. HeidiSQL to see if your server admits remote connections, because you will need to connect from your android app.
 

ilan

Expert
Licensed User

aeric

Well-Known Member
Licensed User
thanx for all answers.



this is my phpcode:

B4X:
<?

$table = "";

include ("");
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$action = $_GET["action"];
 
switch ($action)

{

    Case "GetUserName":
        $username = $_GET["username"];
        $q = mysql_query("SELECT password FROM $table WHERE username='$username'");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
    break;
  
    case "SignUp":
        $username = $_GET["username"];
        $password = $_GET["password"];
        $q = mysql_query("INSERT INTO $table (username, password) VALUES ('$username', '$password')");
        print "Inserted";
    break;
  
  
}


?>
b4a code:

B4X:
Dim GetPersons As HttpJob
        GetPersons.Initialize("GetUserN", Me)
        GetPersons.download2(domain & "8bitlogin.php", Array As String ("action", "GetUserName", "username", user))
if JobDone event i am getting the data and showing to the user...



View attachment 79051

is this what you mean erel?
You removed the table name and include file in your PHP file for demo purpose?

By the way, I am no longer using mysql_connect or mysql_query (or even mysqli) as my shared hosting complain in the error log that the function is deprecated in new PHP version. I am using PDO and stored procedure (parameterised query) now. I think you should take a look at it.
 
Last edited:

ilan

Expert
Licensed User
You should use a MySQLi Class.
i have very low experience with sql so i dont know what is mysqli. do you mean a b4a class?
are the database the same or are they different?

thank you
 

José J. Aguilar

Well-Known Member
Licensed User

DonManfred

Expert
Licensed User

ilan

Expert
Licensed User
For the communication with PHP you only need okhttputils.
this is what i am doing today.

If you want to access MySQL directly from B4A then you should use https://www.b4x.com/android/forum/threads/jdbcsql-directly-connect-to-remote-databases.84016/ though the best solution is for sure to use JRDC2.
what i have understood is that i will need for that a hosting that support remote access and shared hosting does not support it.
can you recommend on a hosting that support remote access?
 
Top