Android Question Connecting to MySQL

erasmusackon

Member
Licensed User
Longtime User
Am getting this error when connecting to mysql using PHP script. Need help.

[15-Jun-2017 04:58:55 CST6CDT] PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /xxx/xxx/public_html/app/sikacon.php:8
Stack trace:
#0 {main}
thrown in /xxx/xxx/public_html/app/sikacon.php on line 8

This is the scripts.

<?php

$host = "xxxxx";
$user = "xxxxx";
$pw = "xxxx";
$db = "xxxxx";

$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 "CountPersons":
$q = mysql_query("SELECT * FROM DepositTransactions");
$count = mysql_num_rows($q);
print json_encode($count);
break;

Case "GetPersons":
$q = mysql_query("SELECT AccountNumber, MemberID FROM DepositTransactions");
$rows = array();
while($r = mysql_fetch_assoc($q))
{
$rows[] = $r;
}
print json_encode($rows);
break;

case "InsertNewPerson":
$name = $_GET["name"];
$age = $_GET["age"];
$q = mysql_query("INSERT INTO UserRegister (CustomerName, CustomerNo) VALUES ('$name', $age)");
print json_encode("Inserted");
break;

}

?>
 

OliverA

Expert
Licensed User
Longtime User
@OliverA Thanks. Am was trying to get the bottom of the error and currently the hosting service provider is checking why am getting Error:javax,net.ssl.SSLHandshakeExecption:SSL handshake aborted, I/O during system call, connection reset by peer.
What version of Android are you testing?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
he used a depricated method(mysql_connect) .. then op switched to manfreds sample code that works fine but the server now is the problem,
I'm thick today, need more coffee... I guess I would like to have seen:

I tried this code, using the web browser I get the following result (some result) but my app produces an error.

This way I know that the back end works, what it produces, and that settles the whole does the backend work question(s)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
@erasmusackon

your B4A code had originally this
B4X:
GetPersons.download2("http://" & ServerIP & "/app/sikacon.php", Array As String ("action", "GetPersons"))

and I'm guessing you switched it to this
B4X:
GetPersons.download2("https://" & ServerIP & "/app/sikacon.php", Array As String ("action", "GetPersons"))
(just change the http:// to https://)

So are we saying that the first (original, http://) connection works, but the new (https://) connection is not working?
 
Upvote 0

erasmusackon

Member
Licensed User
Longtime User

Both http:// and https:// are not working
 
Upvote 0

erasmusackon

Member
Licensed User
Longtime User
Thanks @Erel. That also didn't work but finally this is what worked for me. I hope it will be helpful to others as well.

B4X:
<?php

$databasehost = "www.xxxx.com";
$databasename = "xxxxDB";
$databaseusername = "xxxx2";
$databasepassword = "xxxx";

$con=mysqli_init();
if (!$con)
  {
  die("mysqli_init failed");
  }

mysqli_ssl_set($con,"key.pem","cert.pem","cacert.pem",NULL,NULL); 

if (!mysqli_real_connect($con,$databasehost,$databaseusername,$databasepassword,$databasename))
  {
  die("Connect Error: " . mysqli_connect_error());
  }

// Some queries...
    $q = mysqli_query($con,"SELECT * FROM DepositTransactions WHERE MemberID=40001762");
    $rows = array();
    while($r = mysqli_fetch_assoc($q))
    {
        $rows[] = $r;
    }
    print json_encode($rows);
    mysqli_close($con);
mysqli_close($con);

$action = $_GET["action"];

switch ($action) 

{
    case "CountPersons":
        $q = mysqli_query($con,"SELECT * FROM DepositTransactions WHERE MemberID=40001762");
        $count = mysqli_num_rows($q);
        print json_encode($count);
        mysqli_close($con);
    break;
   
    Case "GetPersons":
        $q = mysqli_query($con,"SELECT * FROM DepositTransactions WHERE MemberID=40001762");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
        mysqli_close($con);
    break;
   
    case "InsertNewPerson":
        $name = $_GET["name"];
        $age = $_GET["age"];
        $q = mysqli_query($con,"INSERT INTO UserRegister (CustomerName, CustomerNo) VALUES ('$name', $age)");
        print json_encode("Inserted");
        mysqli_close($con);
    break;
   
}
?>
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

erasmusackon

Member
Licensed User
Longtime User
So the server issue is fixed? If so, what was the issue?

The SSL is enabled on the server so only the connection below is able to access the server. Thank you.

B4X:
mysqli_ssl_set($con,"key.pem","cert.pem","cacert.pem",NULL,NULL); 

if (!mysqli_real_connect($con,$databasehost,$databaseusername,$databasepassword,$databasename))
 {
 die("Connect Error: " . mysqli_connect_error());
 }
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Interesting. I guess I did not make myself at all clear when I asked you to access the site via http / https, because then we would have figured this out yesterday. What I was wanting you to do was use a browser to access the site just like your android app. Meaning I thought you were using this in your address bar of the browser
B4X:
http://xxx.xxx.xxx.xxx/app/sikacon.php?action=GetPersons
where xxx.xxx.xxx.xxx is your server's IP address (same as ServerIP in you android app). If that would have been done, we should have gotten some sort of error message from the script. That was the point I was trying to make - that without knowing if the script actually runs properly, one cannot determine what is wrong with your app. I guess I need to really, really work on my communication skills.

Good luck with your project.
 
Upvote 0

erasmusackon

Member
Licensed User
Longtime User

Thanks. Your support really helped and hope to get more from whenever necessary since you started IT before i was born.
Most grateful
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…