By using PHPMyAdmin, we can first create the result we want to have in our code, and then copy the SQL into PHP and query it with mysql_query($sql). However we need to save the result for further data retrieval. Next, we need to count how many rows there are as an error prevention measure with mysql_num_rows($result), and then find the rows one at a time with mysql_fetch_array($result) within a loop.
Take a look at the source after the jump to see what I mean, or just watch the video.
Source used after the jump;
It is good that this tutorial didn’t have any odd flickering this time.
<?php
$link = mysql_connect('localhost', 'phpuser', 'phppass');
if (!$link) {
die('Not connected : ' . mysql_error());
}else{
//echo "I am connected. ";
}
// make foo the current db
$db_selected = mysql_select_db('test', $link);
if (!$db_selected) {
die ('Can\'t use test : ' . mysql_error());
}
$table = "tableish";
$sql = "SELECT * FROM `tableish` ORDER BY `tableish`.`ID` ASC LIMIT 3";
$result = mysql_query($sql, $link);
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)){
echo $row['name']."<br /><blockquote>".$row['content']."</blockquote>\n";
}
}
?>