Sunday, August 17, 2008

A basic Select statement with mysql and php

A basic select statement with mysql and php

****************************
//// means < \\\\ means >
****************************

////?
//define $result - what to select
$result = "select * from table_name";

//connect to database
mysql_connect("username","password","") or die ("could not connect to database.");
mysql_select_db("test") or die("error selecting db");
$result = mysql_query($query) or die (mysql_error());

//count number of result colums
$number_colums = mysql_num_fields($result);

//draw table
echo "////table\\\\";
echo "////tr\\\\";

//generate header column using loops
for ($i=0; $i<$number_colums; $i++)
{
echo "////td\\\\".mysql_field_name($result, $i)."/////td\\\\";
}
echo "/////tr\\\\";

//generate results table body
while ($row = mysql_fetch_row($result))
{
echo "////tr\\\\";
for ($i=0; $i<$number_colums; $i++)
{
echo "////td\\\\";

//test if have null value then generate null words
if (!isset($row[$i])){
echo "null";
}else{
echo $row[$i];
}
echo "/////td\\\\";
}
echo "/////tr\\\\";
}
echo "/////table\\\\";
?\\\\

0 comments: