Ubuntu – PHP and MySQL not communicating

MySQLPHP

I have come to the conclusion that my PHP installation and my MySQL installation are not getting along; I’m working on making my first web page that uses MySQL for content, but after two days, man pages, online documentation, and dozens of tutorials, I just can’t get the PHP/MySQL part to work. I have an established home web server (I am the only admin/user) that runs Apache and hosts my web pages. I have MySQL installed, and was able to create and query a database through Terminal. I have PHP installed, and can use echo to print text to the document. When I work with MySQL however, everything changes…

I have a page that contains initialization stuff in . contains a few paragraphs of hardcoded HTML and then PHP code to access MySQL. When I first loaded the page, all that resulted was a blank page. I commented out the PHP section and reloaded to confirm that the plain HTML still worked, which it did. Therefore, some part of PHP was keeping any of the page from ever loading.

To begin troubleshooting my code, I left all of the PHP code commented except the following code for the server connection, which is adapted from several different tutorials:

<?
echo("<p>PHP is working</p>"); 
$ses = mysql_connect("localhost","username","password");
if(!$ses){
 echo("<p>Connection to content server failed.</p>");
 exit();
}
echo("<p>Database Connected</p>");
?>

When the page is loaded, the beginning HTML loads in addition to a paragraph reading “PHP is working” but neither the failure or success message appear. I removed the If structure from the code – so that only two echos and the mysql_connect remained – and got the same result.

It seems that the mysql_connect feature is causing the server to stop loading the code when it is run, but I’m out of ideas for how to fix it. For the record: I’ve modified the php.ini file to include the “connection = msql.so” line, and I’m using MySQL 5.5, Apache 2.2.22, PHP 5.3.10, Ubuntu 12.04. Also, both my router and modem have been configured to forward port 3306 to the server – though I don’t see how that should be necessary – and my MySQL username and password have been triple checked and are identical to the ones that I use successfully in Terminal.

What am I missing?

Best Answer

Everything looks OK so I think your installation is not complete. For mysql and php to communicate, you need to install the php5-mysql package :

sudo apt-get install php5-mysql
Related Question