Cant Connect to MYSQL database from PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JC2710
Forum Newbie
Posts: 6
Joined: Sun May 03, 2009 11:11 am

Cant Connect to MYSQL database from PHP

Post by JC2710 »

Hi

Im new to PHP and am trying to connect to MYSQL database but all I get is blank page!

My Code is:

Code: Select all

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
 
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
But all I get is a blank page. I have run this code which works:

Code: Select all

<?php
 echo 'PHP is working.<br>';
 echo phpinfo();
?>
So it must be to do with connecting to the database. I cant understand why I dont even get an msg saying "Unable to connect to MySQL".

Any ideas?

Thanks
Last edited by Benjamin on Sun May 03, 2009 12:50 pm, edited 1 time in total.
Reason: Changed code type from text to php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Cant Connect to MYSQL database from PHP

Post by ldougherty »

Hello,

Whenever you get a blank screen back it is likely that you have a syntax error on your page and the PHP directive display_errors is disabled.

Try using this connection string..

$db = mysql_connect("$hostname", "$username", "$password");
if (!$db) { die('Could Not Connect'.mysql_error()); }
mysql_select_db("$dbname",$db);

This should give you the exact mysql error in the event that you are not able to make a connection.

You can also attempt to put this at the top of the page just below <?php

ini_set('display_errors', 1);

That will enable display_errors assuming you have permission to do so on your web server.

The other things you can attempt to change are localhost with 127.0.0.1 and obviously you'll want to make sure you supply a password for your $password variable.

Hope this helps out..
JC2710
Forum Newbie
Posts: 6
Joined: Sun May 03, 2009 11:11 am

Re: Cant Connect to MYSQL database from PHP

Post by JC2710 »

Thanks

Ive managed to get the error now.

Fatal error: Call to undefined function mysql_connect()

Which is something to do with not having php_mysql.dll or adding it as extension to php.ini file.

I still cant get it to work.

Any ideas on where to put php_mysql.dll or how to edit php.ini file?

Thanks
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Cant Connect to MYSQL database from PHP

Post by ldougherty »

Is this on a Windows or Linux server? Is it a shared hosting server or something that you actually have full terminal access to?
Post Reply