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.

 

Link to comment
Share on other sites

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 );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.