Android Question [Solved]Unable to connect to MySQL-Database on local net.

opus

Active Member
Licensed User
Longtime User
Hi,
I'm trying to connect to a MySQL-Database im my local network using the Tutorial https://www.b4x.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/
I only get an "Internal Server Error" from Job.Result?

The File is found (when using it without the "php" in the starting line, the code of the php-file is returned)
Do I need to use a User that is allowed the connect from the device where the webserver and the database, or do I have to use as Usere that is allowed to connect from a remote IP? Tested both, same result?

I'm out of ideas?
 

DonManfred

Expert
Licensed User
Longtime User
The File is found (when using it without the "php" in the starting line, the code of the php-file is returned)
Are you sure the webserver is able to run php scripts?
Sounds like the webserver does not know the extension and does not know what to do. So it is returning the file (as you are requesting the file)
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
The Server did run the test using an index.php containing:
B4X:
<?php
Echo "Something";
?>
I did get "Something" when "pointing" the browser the IP of the server
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
I narrowed it down to the connection,
having an Echo before each different line in the PHP file and run just the file from the browser, I got all feedbacks before the "..mysql_connect....".

??
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
before the "..mysql_connect....".
then you should output the error if the connection can not be established!?

PHP:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('connection error: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
Funny, nothing (new) in the mysql_error.log.
Last entry was:
B4X:
 160206 20:09:50 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.44-0+debu8u1-log' socket: '/var/run/mysqld/mysqld.sock' port:3306 (Raspbian)
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
B4X:
<?php
Echo "Start";
$databasehost = "localhost";
$databaseusername = "root";
$databasepassword = "opus";
$databasename = "openhab";
Echo "Before Connect";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword);
Echo "After Conect";
mysql_select_db($databasename) or die(mysql_error());

$query = file_get_contents("php://input");
$sth = mysql_query($query);

if (mysql_errno())
{
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth))
{
$rows[] = $r;
}
print json_encode($rows);
}
?>
Using a browser I only get "StartBefore Connect", in my understanding the connection isn't even tried.

??
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
@DonManfred:
I'm sorry, the last listing posted was not the one I changed according your suggestion. During all those test, I got mixed up what to post.
The outcome of the test with your suggestion is in my Post#8.
And THANK YOU for the help given so far.
When it comes to MySQL I'm a beginner, I'm sorry for that.
As I see it no connection is made or even tried (no entry in errorlog). When connection to the database from the device on which the database is everything works fine.
In my understanding (which could be wrong) the connection using this php-script would also be done locally. Is that correct?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
add this right after your $con= line

B4X:
if (!$con)  {  die('Could not connect: ' . mysql_error()); }

and tell us the error you get
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
Thanks sorex, thats what DonManfred already said. Me stupid didn't put that in the posted listing. I tried that already.
It looks like I'm missing MySQL-Functions in PHP. I'm searching to install those.
Will report back.

Lots of things to learn for me in this one!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I guess you are using PHP 5.5+

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
PHP:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

https://www.google.de/?gws_rd=ssl#q=php deprecated mysql_connect()

http://php.net/manual/en/migration55.deprecated.php
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
the method you use is outdated but still included in mosts install.

if not then you should use the mysqli methods.
 
Upvote 0

opus

Active Member
Licensed User
Longtime User
That's it:
"Call to unidentified function mysql_connect().."

Should I use an older PHP-Version or use these "mysqli methods"?
 
Upvote 0
Top