Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
1  
Replace 'username' and password with correct username(probably root) and password –  Tachyons Dec 24 '12 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. –  Casey Hungler Dec 24 '12 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. –  Casey Hungler Dec 24 '12 at 1:38
1  
Did you install php5-mysql? –  laurent Dec 24 '12 at 1:40
    
I'm pretty sure, but I'll try it again. –  Casey Hungler Dec 24 '12 at 1:41

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer

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

share|improve this answer
    
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? –  Casey Hungler Dec 24 '12 at 2:03
    
MySQLi is an improved version. –  owl Dec 24 '12 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 '12 at 2:09
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.