I am trying to set up a script that queries a database to populate a page. This is my first foray into database querying in PHP, so forgive me if the problem is something really stupid.
The problem I have is that the page does not seem to connect to the database, as the first 2 debug statements are rendered in the browser, but nothing after them is. however, when I run it on the command line using php.exe, it runs properly and displays all required data from the query.
I am running wampserver 2.2 on windows 7 and it does not display in any browser (tested in Chrome, Firefox, and IE9.
My current code is below. I have also tried moving the connection to the head, but that prevents anything, even debug1, from rendering. The die statement is not rendered in any circumstance.
<html>
<body>
debug1<br />
<?php
echo "debug2<br />";
$con = mysql_connect("hostname.of.database","username","password") die('Could not connect: ' . mysql_error());
echo "debug3<br />";
mysql_select_db("dbname",$con);
$result = mysql_query("SELECT * FROM Mail");
while ($row = mysql_fetch_array($result))
{
echo $row['Sender'];
echo "<br />";
echo $row['Subject'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo "<br />";
}
echo "debug4<br />";
mysql_close($con);
?>
</body>
</html>
EDIT
After moving the die statement to the same line as the connection, I get the following rendered in the page:
Fatal error: Call to undefined function mysql_connect() in C:\wamp\www\test.php on line 10
EDIT
I have added extension=php_mysql.dll to my php.ini file and restarted the servers. I am now getting the following page:

EDIT
Problem solved. My last problem was that I forgot to replace hostname.of.database with the actual hostname.
For those who read this in the future, the solution is as follows:
- don’t close the connection before retrieving the results
- ensure that the php_mysql extension is enabled by adding
extension=php_mysql.dllto the php.ini file - reboot the server, clear the browser cache and try again.
Because you’re closing the connection before fetching results:
Unless you specifically have a need to free some memory or destroy a connection when you intend to create another one, it is usually not necessary to make explicit calls to
mysql_close(). It will be called when the script completes execution.Update:
After posting your error message, it would appear that the MySQL extension is not enabled for your WAMP stack. A different php.ini file may be used by the command line than is used by the web server. Locate the correct php.ini by calling
phpinfo()and enable the MySQL extension.