0

I'm currently trying through a php to connect to a database in another machine, I'm able to connect already using the command line, like this:

mysql -u root -p'' -h 192.168.2.2 proyectoti

I'm able to manage the database -DDL and DML work-, now the thing is I need to create a simple web page to interact with the database (listing and adding elements to it) and the web page and DB must be in different machines, here is my php:

<?php
//Connect To Database
$hostname='192.168.2.2';
$username='root';
$password='';
$dbname='proyectoti';
$usertable='prueba';
$yourfield = 'prueba';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

$query = 'SELECT * FROM ' . $usertable;
$result = mysql_query($query);
if($result) {
    while($row = mysql_fetch_array($result)){
        print $name = $row[$yourfield];
        echo 'Name: ' . $name;
    }
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

My database is just for testing and it only has a table called 'prueba' with a field called 'prueba', both machines run CentOS 7 (virtualized), are able to communicate between them (ping), the machine with the DB has installed XAMPP and I already tried a simple page to access something in my demo DB (locally) and it works, the machine that is supposed to have the webpage has httpd installed and php enabled (Already tested this by executing phpinfo(), it works). When I try to load the php on the web browser it just shows a blank page, What should I do? Thank you!

  • 1
    **Run! Run in terror!** (Even on a private LAN, passwordless root access to a database is not a good idea). – Quentin Mar 25 '18 at 19:09
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 25 '18 at 19:09
  • 2
    It really isn't clear what the problem is here. You've described a bunch of things that you have done which *work*, but not said what fails or how. Do you get errors? What are the errors? – Quentin Mar 25 '18 at 19:10
  • I get a blank page on the web browser, it should show one record I have in the DB but it's just blank. – Santiago Troitiño Cristancho Mar 25 '18 at 19:14
  • Because I'm just testing I will later create a proper user with a password for security. – Santiago Troitiño Cristancho Mar 25 '18 at 19:14
  • Wait, I installed a package and restarted the service and now I get: "Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.2.2' (13) " (Using PDO). – Santiago Troitiño Cristancho Mar 25 '18 at 19:42

1 Answers1

0

I was finally able to connect using php, I did the following:

  1. On the command line I ran: yum install php-mysql and restarted httpd service. I recieved the following error: Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on (13), so I did as follows:
  2. On the command line I ran: setsebool httpd_can_network_connect_db=1 That let me connect to the database without any problem, I used the PDO example in the following page:

https://www.w3schools.com/php/php_mysql_connect.asp