View Single Post
  #2 (permalink)  
Old February 16th, 2012, 08:08 AM
UseLess UseLess is offline
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Code:
$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
	// $result is false and this indicates
	// the query failed so output a message
	// optionally stop processing script here
}
else
{
	// query was successful
	// so start a loop to grab each row from the database
	// the loop will end when there are no more rows to be returned
	// in this case $row will be false
	while( $row = mysql_fetch_array($result);
	{
		// do something with the data
	}
}
Another way to do the above but this time testing to see if anything is returned

Code:
$sql = 'SELECT * FROM table';
if( !$result = mysql_query($sql) )
{
	// $result is false and this indicates
	// the query failed so output a message
	// optionally stop processing script here
}

// test to see if anything was returned by the query
if( $row = mysql_fetch_array($result) )
{
	// db returned something so process it
	do
	{
		// process data
		// data will be in the $row variable used above
		
		// do while loops run once as the test is done below to determine
		// if the loop should actually loop
	}
	while( $row = mysql_fetch_array($result) );
}
else
{
	// optional
	// display message that nothing was returned from db
}
Reply With Quote