johnmerlino Posted August 30, 2010 Share Posted August 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
johnmerlino Posted August 30, 2010 Author Share Posted August 30, 2010 Here is the parameter I am passing into the show function: $book = show('books', 1); echo $book['title'].'</ br>'; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 30, 2010 Share Posted August 30, 2010 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 ); Quote Link to comment Share on other sites More sharing options...
johnmerlino Posted August 30, 2010 Author Share Posted August 30, 2010 The mysql_error() made it obvious that I was missing quotes around %s. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.