PHP Tutorials
Display records from MySQL
Table: my_quotes
| quote_id | quote | auth_fn | auth_mn | auth_ln | subject |
| 1 | I am beginning to suspect all elaborate and special systems of education. They seem to me to be built up on the supposition that every child is a kind of idiot who must be taught to think. | Anne | Sullivan | education | |
| 2 | Creativity is a type of learning process where the teacher and pupil are located in the same individual. | Arthur | Koestler | education | |
| 3 | Education is a better safeguard of liberty than a standing army. | Edward | Everett | education | |
| 4 | Education is what remains after one has forgotten everything he learned in school. | Albert | Einstein | education |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Display my Quotes</title>
<style type="text/css">
<!--
body {background: #fff;}
#container {margin: auto; width:700px;}
.quote {background: #F0F0E1; padding: 1em; font: 19px Georgia, "Times New Roman", Times, serif;}
.author {text-align: right; margin-bottom: 2em; font: 10px Verdana, Geneva, sans-serif;}
-->
</style>
</head>
<body>
<div id="container">
<?
include("my_connection.php");
$myQuery = mysql_query("SELECT * FROM my_quotes LIMIT 0, 10") or die (mysql_error());
while($myRow = mysql_fetch_object($myQuery))
{
echo "<div class='quote'>" . $myRow -> quote . "</div>";
echo "<div class='author'>" . $myRow -> auth_fn . " " . $myRow -> auth_mn . " ". $myRow -> auth_ln . "</div>";
}
?>
</div>
</body>
</html>
mysql_connect("your-mySQL-server.yourdomain.com","your-user-name","your-password") or die ("your own error message");
mysql_select_db ("your-database-name") or die ("your own error message");
?>
About the include file
An include command adds content of the external file on the current page. If you'd rather have a connection with your database on every page, just replace the include line with the content of the external file, for example:
<?
mysql_connect("your-mySQL-server.yourdomain.com", "your-user-name", "your-password") or die ("your own error message");
mysql_select_db ("your-database-name") or die ("your own error message");
$myQuery = mysql_query("SELECT * FROM my_quotes LIMIT 0, 10") or die (mysql_error());
while($myRow = mysql_fetch_object($myQuery))
{
echo "<div>" . $myRow -> quote . "</div>";
echo "<div>" . $myRow -> auth_fn . " " . $myRow -> auth_mn . " ". $myRow -> auth_ln . "</div>";
}
?>
About error messages
If there is an error connecting to the database you can show your own message, or the error message from MySQL server. The error message from the server is mysql_error() and the custom error message is written between quotes like this "this is my error message"
I'd advise you to use "your own error message" for the production server. The visitor doesn' t need (or care) to know errors in your code:
mysql_connect("your-mySQL-server.yourdomain.com", "your-user-name", "your-password") or die ("your own error message");
mysql_select_db ("your-database-name") or die ("your own error message");
?>
Use mysql_error() for the testing server. You have to know what these errors are:
mysql_connect("your-mySQL-server.yourdomain.com", "your-user-name", "your-password") or die (mysql_error());
mysql_select_db ("your-database-name") or die (mysql_error());
?>
You can also combine error messages:
mysql_connect("your-mySQL-server.yourdomain.com", "your-user-name", "your-password") or die ("Sorry, there is an error" . mysql_error());
mysql_select_db ("your-database-name") or die ("your own error message");
?>
The dot . is used in PHP the same as ampersand & in ASP or plus + in JavaScript.
Hiding all errors on the page
Another method of hiding all error messages is the following code:
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'Off');
?>
You can add the code above on the top of your PHP page, above everything else, above the HTML code:
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'Off');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>