Hi guys,
I am somewhat competent with HTML, CSS and Javascript, now I'm trying to start using PHP. I've downloaded and installed Apache, PHP and MySQL and they kind of work. I can use MySQL on its own fine, and PHP on its own fine (I can run phpinfo, echo Hello World etc. and can visit pages on localhost). However I can't get my PHP script to work using MySQL. I copied out *exactly* a script from O'Reilly's PHP and get the following errors printed out:
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\AppServ\www\mysql2.php on line 14
Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\AppServ\www\mysql2.php on line 14
2002
Warning: mysql_select_db() [function.mysql-select-db]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\AppServ\www\mysql2.php on line 21
Warning: mysql_select_db() [function.mysql-select-db]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\AppServ\www\mysql2.php on line 21
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\AppServ\www\mysql2.php on line 21
Fatal error: Maximum execution time of 60 seconds exceeded in C:\AppServ\www\mysql2.php on line 21
Here is the script, the password, host etc. is definitely correct, I've double checked in MySQL itself.
Is there anything obvious I'm doing wrong? Any help would be very much appreciated. Thanks, BobPHP Code:<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
$host = "localhost";
$user = "root";
$pass = "elephant";
$db = "favourite_foods";
// open connection
$connection = mysql_connect($host, $user, $pass); /*THIS IS LINE 14!!!!*/
if(mysql_error()) {
echo mysql_errno();
}
// select database
mysql_select_db($db) or die ("Unable to select database!"); /*THIS IS LINE 21!!!!*/
// create query
$query = "SELECT * FROM favourite_food";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result)>0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>" . $row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
</body>
</html>



Reply With Quote


Bookmarks