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<br>";
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

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

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

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

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

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

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

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

You can also add:

error_reporting (E_ALL);

Which is similar but a little different.

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

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)

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

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);
?>
Re: Cannot connect to MySQL from PHP. No error message is displayed 80 80

u r missing the mysql_select_db()

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

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();
}
Re: Cannot connect to MySQL from PHP. No error message is displayed 80 80

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.

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

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

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

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

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

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.