0

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

Edited by Richtofen345: n/a

8
Contributors
12
Replies
28
Views
7 Years
Discussion Span
Last Post by Deepika_6
0

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

0

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)

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);
?>

Edited by Chi-Town: n/a

0

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

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.

0

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

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.