Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given..


johnmerlino

Recommended Posts

Hey all,

 

The mysql_query function is failing in the show function and I'm not sure why:

 

function index($item){
	db_connect();
	$query = "SELECT * FROM $item ORDER BY $item.id DESC";
	$result = mysql_query($query);
	$result = db_result_to_array($result);
	return $result;
}

function show($item, $id){
	db_connect();
	$query = sprintf("SELECT * FROM $item WHERE $item.id = '%s", 
		mysql_real_escape_string($id));
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	return $row;
}

//Stuff that belongs in view
 $books = index('books');	

foreach($books as $book){
	echo $book['title'].'</ br>';
}

$book = show('books', 1);
echo $book['title'].'</ br>';

 

Thanks for any response.

 

This error usually occurs due to one of the following

  • You're not connected to the database
  • The query returned no results as there is no records with the id of 1
  • Or your query failed due to an error.

 

To see if their is an error use the function mysql_error, eg

		$result = mysql_query($query) or trigger_error('MySQL Error: ' . mysql_error() . '<br />Query: '  . $query, E_USER_ERROR );

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.