Bring iT

Wednesday, July 02, 2008

MySql : View Data from Database

Now we want to retrieve the data from the database and display them as links on the page. The SQL statement used to get data from a database is SELECT. The format is:
SELECT (columns) FROM (table)
WHERE (exclusive criteria)

For our database we want so select all columns, so a '*' is used instead of listing out each column. We also only want to select a specific category of links, let's say "Local Docs" is the cateogry we want. So our SELECT SQL statement would be:
SELECT * FROM links
WHERE category = 'Local Docs'

Single quotes specify a string value in the WHERE clause, if we were using a column which was a number value no quotes would be needed. The WHERE part is optional, if you want to select everything from the database you can leave off the WHERE portion. Review the SQL Tutorial for more examples of what can be done with SELECT statements and WHERE clauses.



The script to display links out of the database starts with the usually code to initialize the database connection.
$usr = "--username--";
$pwd = "--password--";
$db = "linksdb";
$host = "localhost";

$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
?>

The next part of the script is setting up and executing the SQL statement. This should look familiar to the previous pages. I set the category as a variable at the beginning so the code can be copied and pasted for other category selects and only one change is needed.
$category = "Local Docs";

$SQL = " SELECT * FROM links ";
$SQL = $SQL . " WHERE category = '$category' ";
$retid = mysql_db_query($db, $SQL, $cid);
if (!$retid) { echo( mysql_error()); }

The SELECT statement returns rows of data from the database. We need to loop through that data and display the information we want, which will be our links. The command to grab a row is:
$row = mysql_fetch_array($retid);

This sets $row as an array holding one record from the database, with the column names as the "keys" for the array. So to retrieve the siteurl value from that array you would use:
$siteurl = $row["siteurl"];

When the mysql_fetch_array command is called next, it moves to the next data record returned by the SELECT. If there are no more rows of data the command returns false. So to loop through all rows of data we can setup a while statement with the mysql_fetch_array in it. So here is the code to loop through the data and display it to the screen:
while ($row = mysql_fetch_array($retid)) {
$siteurl = $row["siteurl"];
$sitename = $row["sitename"];

echo ("
$sitename
\n");
}
echo ("");

?>

You can download the complete script of viewing data from clik the title of this posting

3 Comments:

  • xtsav [url=http://www.beatsheadphonesuksale.co.uk]beats by dre[/url] nyxmgtkx http://www.beatsheadphonesuksale.co.uk wkwkmambn dinjdn [url=http://www.cheapbeatsukheadphonesale.co.uk]beats by dre[/url] clbfjtlnhttp://www.cheapbeatsukheadphonesale.co.uk snwwjaeme vawezy [url=http://www.cheapbeatsbydreuksales.co.uk]cheap beats by dre[/url] fmnehfbk http://www.cheapbeatsbydreuksales.co.uk pcizqqhxr zvrocw [url=http://www.cheapbeatsbydresaleuk.co.uk]dr dre beats[/url] nplfosxa http://www.cheapbeatsbydresaleuk.co.uk rbvuozlwi puspte [url=http://www.cheap-beatsbydreuk.co.uk]dr dre beats[/url] gqvuebnu http://www.cheap-beatsbydreuk.co.uk brsvjtnph bnnbvx [url=http://www.beatsbydrdreukonsale.co.uk]dr dre beats[/url] vqgteqfv http://www.beatsbydrdreukonsale.co.uk zavbebbcf w

    By Anonymous Anonymous, At 4:40 PM  

  • tczyj [url=http://www.beatsheadphonesuksale.co.uk]beats by dre[/url] gxzzhyqa http://www.beatsheadphonesuksale.co.uk veeqbndrp dpwzih [url=http://www.cheapbeatsukheadphonesale.co.uk]beats by dre[/url] nqyvlvmfhttp://www.cheapbeatsukheadphonesale.co.uk xtraidtge vycupm [url=http://www.cheapbeatsbydreuksales.co.uk]dr dre beats[/url] etdhbgjd http://www.cheapbeatsbydreuksales.co.uk dtxwmgatw awlfrc [url=http://www.cheapbeatsbydresaleuk.co.uk]cheap beats by dre[/url] dfutptdp http://www.cheapbeatsbydresaleuk.co.uk swgpzftgc heoxeo [url=http://www.cheap-beatsbydreuk.co.uk]dr dre beats[/url] rumgndnx http://www.cheap-beatsbydreuk.co.uk gbttqjbxv ecnfuk [url=http://www.beatsbydrdreukonsale.co.uk]dr dre beats[/url] xkvjptzh http://www.beatsbydrdreukonsale.co.uk kylfgmsxk m

    By Anonymous Anonymous, At 1:59 PM  

  • Perhaps there is а massive pluѕ fοr myѕelf аnԁ waѕtіng buѕіnеss а lοt оf money.
    Things like yоur tools and equipment to
    proԁuce dіesel for Europe and Asia is very сгitical.
    It's a fairly modest overall impact however when business you look at workforce rebalancing, I think descriptive of the kind of support that notion. Researchers business at the Federal Reserve recently announcing that unemployment rates won't dip much
    until 2014, you'll more than likely not be interested.

    Also visit my webpage - blog internet marketing

    By Anonymous Anonymous, At 3:23 AM  

Post a Comment

Subscribe to Post Comments [Atom]



<< Home