I am trying to connect to mysql from php Below is the sample code i used.

$username = "root";
$password = "pwd";
$hostname = "localhost";

$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";


The problem here is I am not getting any error message. The page does not display anything. But when i comment the mysql_coonect() line i can see the prints and echo's. I tried checking if $dbh is valid and also tried putting the code in an exception block. But nothing helps. I have tried many variations of the above code to no effect.

Please help me to identify the problem.

I am able to execute php scripts which shows that my php install is fine.
I am also able to login to mysql db using (locaalhost, root,pwd) in command prompt and using mysql workbench.

Php version = php-5.2.13-win32-
MySql version = mysql-essential-5.1.48-win32


Recommended Answers

if you have two <?PHP commands at the start as shown, get rid of one of them and see what you get.

Jump to Post

Maybe the PHP configuration on the server running your script tells PHP not to display any errors (although I don't see any errors in your script.) Try adding the following line to the beginning of your script: ini_set('display_errors', 1);

Jump to Post

You can also add:

error_reporting (E_ALL);

Which is similar but a little different.

Jump to Post

All 12 Replies

if you have two <?PHP commands at the start as shown, get rid of one of them and see what you get.

Its a type. There is only one <?php command actually.

You can also add:

error_reporting (E_ALL);

Which is similar but a little different.

After adding the lines mentioned by d5e5 & Chrishea i can see the error message. Thanks both of you.
I am getting this error message
Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\loginValid.php on line 10

Looks like php cannot recognize my sql. My php install is php-5.2.13-win32-installer.msi.

After googling i found that i need to uncomment the following line from php.ini

But i dont have the above line in my php.ini. It means something is missing even if i put the line i will not be able to get it to work.

I also do not have the extension_dir in my php home ( the line extension_dir is not there in php.ini)

Use the code below to connect to the datsbase. It looks like you dont have mysql_select_db("dataBase_Name",$con); in your code.


$con = mysql_connect("localhost","user_Name","passWord") or die("Check connection");


u r missing the mysql_select_db()

create a short test this will tell you errors atleast, unless it is infact your php not installed.

//connect to server
$link = mysqli_connect('localhost', 'user', 'pass');
if (!$link)
 $error = 'Unable to connect to the database server.';
 include 'error.html.php';

//set database encoding
if (!mysqli_set_charset($link, 'utf8'))
 $output = 'Unable to set database connection encoding.';
 include 'output.html.php';

//select database
if (!mysqli_select_db($link, 'tools'))
 $error = 'Unable to locate the tools database.';
 include 'error.html.php';

Your code looks fine.
It appears your php or mysql has not been installed correctly.
try using a differen connect function such as mysql_pconnect
and re-install your php or mysql.
f it is only that file just download that particular file and see if it works better.
perhaps try a different version of that file.

I found the reason and now its working. It was such a silly blunder on my part.
But before telling that i will tell you what all i did.

I removed php 5 and installed php 4 and downloaded the mysql related dll's.
I went through php.ini and httdconf. I tweaked and rechecked everything still no solution. I was sure something related to mysql is missing.

Then i reinstalled mysql 5 and i noticed the extensions options during install. I had missed to select mysql in it the first time and THATS THE REASON for all the trouble. Once i selected mysql extension everything went fine.

Such a silly thing but so much delay for me. Good thing is i learnt a lot out of this. I am just learning php on my own interest as I am currently in Peoplesoft development.

I wish to convey my thanks to chrishea,d5e5, Chi-Town, ijazkhattak, atrueresistance,metlaix for without your help it would have been much more harder for me!!! :))

No problems glad you got it sorted. :)
Remember to mark threads as solved when solved :)
If your just learning php I suggest challenging yourself. make your website do anything you want. it is the fastest way to learn. :)

the lines which were supposed to display errors are printed as it is in localhost page.......help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.