954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot connect to MySQL from PHP. No error message is displayed

Hi,

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

<?php
<?php
$username = "root";
$password = "pwd";
$hostname = "localhost";

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

?>

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

Thanks
Richtofen

Richtofen345
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

chrishea
Nearly a Posting Virtuoso
1,429 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

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

Richtofen345
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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); http://www.wallpaperama.com/forums/how-to-display-php-errors-in-my-script-code-when-display-errors-is-disabled-t453.html

d5e5
Practically a Posting Shark
812 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

You can also add:

error_reporting (E_ALL);


Which is similar but a little different.

chrishea
Nearly a Posting Virtuoso
1,429 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

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
;extension=php_mysql.dll

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)

Richtofen345
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

<?php

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

mysql_select_db("dataBase_Name",$con);
?>
Chi-Town
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

u r missing the mysql_select_db()

ijazkhattak
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

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';
 exit();
}

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

//select database
if (!mysqli_select_db($link, 'tools'))
{
 $error = 'Unable to locate the tools database.';
 include 'error.html.php';
 exit();
}
atrueresistance
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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.

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

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!!! :))

Richtofen345
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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. :)

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: