Page 1 of 1

Cant Connect to MYSQL database from PHP

Posted: Sun May 03, 2009 11:15 am
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

Re: Cant Connect to MYSQL database from PHP

Posted: Sun May 03, 2009 11:50 am
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..

Re: Cant Connect to MYSQL database from PHP

Posted: Sun May 03, 2009 12:56 pm
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

Re: Cant Connect to MYSQL database from PHP

Posted: Sun May 03, 2009 1:06 pm
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?