The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.
The Flash and PHP Bible has a dedicated forum for support and comments.
Fetch (retrieve) a specific MySQL row and display it in a simple table.
View an Example of this article before you get started.
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['bid']; $query = "SELECT b_name, b_author, b_cat, b_price from lib where id='" . $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
That's it, if you have any questions please leave a comment.
|
Joe Sun Aug 6, 2006 2:03 pm
Your closing the table? why "mysql_close($db_conn);"
It won't do anything as, db_conn variable does not exist, if you ran that variable as your connect it would. Just a little advice to edit, correct me if I'm wrong. |
|
Matthew Sun Aug 6, 2006 2:22 pm
That was an oversight on the explanation portion, thanks for pointing it out.
|
|
salm Wed Nov 29, 2006 12:12 am
Lets suppose james thomas wrote more than one book.What i want to show all the books written by him.
What cod will be required to display this information from mysql database on a page? Please help as i need this urgently. |
|
Matthew Wed Nov 29, 2006 12:38 am
Hope this is a quick enough response. :)
$query = "SELECT b_name, b_author, b_cat, b_price from lib where b_author='James Thomas'"; The rest of the code would be the same. |
|
ferensick Wed Mar 7, 2007 10:38 am
I use mySQL 5.1.14 with php 5.2.0 on my site.
I have a timestamp in my database that stores comments from my site. All I want to do is change the format of the time that is displaying. 'ALTER TABLE `comments` CHANGE `EST_time` `EST_time` TIMESTAMP(10) ON UPDATE CURRENT_TIMESTAMP NULL'; I run this code successfully, but the format does not change. instead of TIMESTAMP(10) it still shows 12... Is this a known bug or is there another way to accomplish this? Thanks in advance. |
|
mkeefe Wed Mar 7, 2007 10:55 am
The safest way to alter a table is to first backup the data. Then you create a new "temp table", assign the new attributes and move the data in. After moving the data make the old table a "temp table" and only delete it after you know the new table is functioning properly.
|
|
ferensick Wed Mar 7, 2007 11:44 am
Thank you for the advice.
Any insight on my primary question? I have been reading on the web about bugs with the mySQL timestamp... I will look more into it and poste the info if relevant. Cheers. |
|
Paul Marsden Tue Nov 13, 2007 4:20 pm
Question. what is the best way to deal with null values? They give me weird effects when displayed in a web page. I have tried printf and echo.
|
|
mkeefe Wed Nov 14, 2007 12:15 pm
There are a couple of options:
1. Set the values to not null in the database will ensure the values are never null 2. Second option is to run a if statement check to see if the variable is in fact null and if so, set it to a valid value. if(is_null($value)) { $value = ""; } |
©2004 - 2008 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN