Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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.

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

![]() |
![]() |
![]() |

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