Computing.Net > Forums > Web Development > Cannot connect to MySQL with PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Cannot connect to MySQL with PHP

Reply to Message Icon

Name: bryboy2002
Date: April 9, 2005 at 16:13:00 Pacific
OS: Windows XP
CPU/Ram: Athlon AMD/448 RAM
Comment:

Hi, I am having a bit of a problem connecting to my MySQL database. Here is the php code I am using:


<html>
<head><title>MySQL Test</title></head>
<body>
<?
/* declare some relevant variables */
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "pass";
$DBName = "info";
$table = "test";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName"); $sqlquery = "SELECT *

FROM $table WHERE opinion = 'Great Job'";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);

$i = 0;

if ($number < 1) {
print "<CENTER>

There Were No Results for Your Search</CENTER>";
}
else {
while ($number > $i) {
$thename = mysql_result($result,$i,"name");
$theemail = mysql_result($result,$i,"email");
print "

Name: $thename
E-Mail:
$theemail</p>";
$i++;
}
}
?>
</BODY></HTML>


When I test the php file, I get the "Unable to connect to database message" indicating exactly just that. What can I do to fix this?



Sponsored Link
Ads by Google

Response Number 1
Name: Khalid
Date: April 10, 2005 at 00:11:06 Pacific
Reply:

First; try to output the mysql-error aswell and use a link-identiefier:
$link_id = mysql_connect($DBhost,$DBuser,$DBpass)
if (!link_id){die("Unable to connect to database".mysql_error());}

Now the script will ouput WHAT went wrong (username unknown, bad password, bad connection, couldnt find host etc)

If mysql could not be found at all, try the following:
Since you are running windows XP, check to see if the mysql-deamon is running: ctrl-alt-delete and look for "mysqld-nt.exe"

If it is not running, you have to start the service manually. Go to Control Panel -> Administrative Tools -> services and make sure the option "mysql" is started or alter the configuration. If "mysql" isnt in the list, try to reinstall mysql.


0

Response Number 2
Name: bryboy2002
Date: April 10, 2005 at 08:24:59 Pacific
Reply:

On your replacement script, should there be a colon after the

$link_id = mysql_connect($DBhost,$DBuser,$DBpass)

stament, making it

$link_id = mysql_connect($DBhost,$DBuser,$DBpass);


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Cannot connect to MySQL with PHP

cannot connect to mysql with php www.computing.net/answers/webdevel/cannot-connect-to-mysql-with-php/2968.html

Cannot connect to MySQL DB with PHP www.computing.net/answers/webdevel/cannot-connect-to-mysql-db-with-php/4085.html

PHP connection to MySQL www.computing.net/answers/webdevel/php-connection-to-mysql/235.html