Querying a mysql database with php example
By liviu - Posted on May 22nd, 2009
This example was taken from
| Programming PHP, 2nd Edition | ||
| By Rasmus Lerdorf, Peter MacIntyre, Kevin Tatroe | ||
| ............................................... | ||
| Publisher: O'Reilly | ||
| Pub Date: April 2006 | ||
| Print ISBN-10: 0-596-00681-0 | ||
| Print ISBN-13: 978-0-59-600681-5 | ||
| Pages: 540 | ||
<?
$connection = mysql_connect("localhost");
$db = "library";
mysql_select_db($db, $connection) or die( "Could not open $db");
$sql = "SELECT * FROM books WHERE available = 1 ORDER BY title";
$result = mysql_query($sql, $connection) or die( "Could not execut sql: $sql");
$num_result = mysql_num_rows($result);
?>
<table cellSpacing="2" cellPadding="6" align="center" border="1">
<tr>
<td colspan="7">
<h3 align="center">These Books are currently available</h3>
</td>
</tr>
<tr>
<td align="center">Title</td>
<td align="center">Publisher</td>
<td align="center">Category</td>
<td align="center">ISBN</td>
</tr>
<?
for ($i=0; $i < $num_result; $i++) {
$row = mysql_fetch_array($result);
$id = $row["bookid"];
echo "<tr>";
echo "<td>";
echo stripslashes($row["title"]);
echo "</td><td>";
if ( !$row["company"] )
{
echo " ";
} else {
echo $row["company"];
}
echo "</td><td>";
echo $row["typedesc"];
echo "</td><td>";
echo $row["isbn"];
echo "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
Bookmark/Search this post with:
- liviu's blog
- Login or register to post comments
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket