| PhotoShop Tutorials | | PHP Tutorials | | CSS Tutorials | | PhotoShop Brushes Downloads | | Fonts Downloads | | Pimp MySpace |



Fetch MySQL Row

Fetch (retrieve) a specific MySQL row and display it in a simple table.

This week we are going to work with a common use of PHP and MySQL, fetching results from MySQL and displaying them

We will first take a look at the code and then explain it in depth

$sql_link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db_name', $sql_link);

$book_id = $_GET['bid'];
$query = "SELECT b_name, b_author, b_cat, b_price from lib where id='" . $book_id . "'";

$result = mysql_query($query);

print "<table>";
while($row = mysql_fetch_array($result)) {
print "<tr><td>" . $row['b_name'] . "</td><td>"
. $row['b_author'] . "</td><td>"
. $row['b_cat'] . "</td><td>"
. $row['b_price'] . "</td></tr>\n";
}
print "</table>";

mysql_close($sql_link);

That is the code, now on to the explanation phase.

$sql_link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db_name', $sql_link);

The first line is used to connect with a MySQL database and the second simply connects to a database. You would input your specific server connection info, which can be obtained by your admin/host.

$book_id = $_GET[&apos;bid&apos;];
$query = "SELECT b_name, b_author, b_cat, b_price from lib where id=&apos;" . $book_id . "'";

$_GET is used to pull in the book id

$result = mysql_query($query);

Actually execute the sql and grab are resultset

print "<table>"; // opening of the table

print "output" an opening to the table

while($row = mysql_fetch_array($result)) {

Loop through the results and print them

print "<tr><td>" . $row['b_name'] . "</td><td>" 
. $row['b_author'] . "</td><td>"
. $row['b_cat'] . "</td><td>"
. $row['b_price'] . "</td></tr>\n";
}

Display the data in a little "non-formatted" table.</table>

print "</table>";

Close the table

mysql_close($sql_link);

Finally close the mysql connection






| SiteMap | | Proxy SiteMap | | Free Mortgage Calculators | Myspace Falling Object | | Myspace Survey | | Myspace Slideshows | |Toms MySpace Editor |
CopyRight 2005 Radialdesigns.co.uk