9

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?

7
  • 1
    Replace 'username' and password with correct username(probably root) and password
    – Tachyons
    Dec 24, 2012 at 1:28
  • 2
    Yeah, I did that. I just didn't think it would be a good idea to throw the username and password out onto the forum. As I said at the end, I have checked the username and password MANY times to make sure that it matches what I use to successfully create, access, and edit the database through Terminal. Dec 24, 2012 at 1:36
  • I've also tried replacing localhost with 127.0.0.1 as well as the local and global IP addresses of the server, but none of them made a difference either. Dec 24, 2012 at 1:38
  • 1
    Did you install php5-mysql?
    – laurent
    Dec 24, 2012 at 1:40
  • I'm pretty sure, but I'll try it again. Dec 24, 2012 at 1:41

3 Answers 3

9

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
1
  • NB: I had to restart the computer before this would work.
    – user124384
    Jul 2, 2015 at 19:34
3

Install php5-mysql package and use mysqli_connect instead of of mysql_connect

4
  • Thanks, someone else suggested that too, and it worked! I'm curious though, in my searches I had run across stuff about mysqli (and PDO was usually in there too), what is it? What makes it better than regular mysql? Dec 24, 2012 at 2:03
  • MySQLi is an improved version.
    – owl
    Dec 24, 2012 at 2:07
  • From what I know, you should use mysqli as it is recommended by the php team and mysql_connect is not anymore in developpement, only maintenance. I think mysql_connect doesn't accept prepared statements for example and many other things.
    – laurent
    Dec 24, 2012 at 2:09
1
sudo apt-get install php5-mysql

Find Tomcat-version from /etc/init.d/. There should be a file with the name tomcat7, hence 7 is the version

sudo service tomcat7 stop 
sudo service tomcat7 start

Sometimes the machine reboot helps in sorting other issues. After reboot, don't forget to run:

sudo service tomcat7 start 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .