designanddev Posted November 26, 2012 Share Posted November 26, 2012 Hi i have this function below.... biut i keep geting this warning message Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean i get this message when i run the function and set it $id, im not sure if the isset in this query is actually workin could someone please help. Kind Regards function get_posts($id = null, $cat_id = null){ global $dbc; $posts = array(); $query = "SELECT b.message as bmsg, b.ID as bid, b.subject, b.post_on, c.ID, c.blog_id, c.message as mgs FROM blog as b LEFT JOIN comments as c ON b.ID = c.blog_id GROUP BY bmsg"; if(isset($id)){ $query .= "WHERE b.ID = '$id'"; } $query = mysqli_query($dbc ,$query); while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ $posts[] = $row; } return $posts; } Quote Link to comment Share on other sites More sharing options...
krisw44 Posted November 26, 2012 Share Posted November 26, 2012 Go into the database, phpmyadmin or whatever it is, and run the query directly with a value for $id that you know should work. When you get that error usually it means that mysql has returned something that isn't a query result so php doesn't know how to handle it. Quote Link to comment Share on other sites More sharing options...
designanddev Posted November 26, 2012 Author Share Posted November 26, 2012 how would i check the if(isset($id)) in SQL query? $query = "SELECT b.message as bmsg, b.ID as bid, b.subject, b.post_on, c.ID, c.blog_id, c.message as mgs FROM blog as b LEFT JOIN comments as c ON b.ID = c.blog_id GROUP BY bmsg"; if(isset($id)){ $query .= "WHERE b.ID = '$id'"; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2012 Share Posted November 26, 2012 The syntax of your query statement is incorrect. The WHERE clause does not go on the end. The following is the basic SELECT query syntax definition (the elements, when present, must be in the order shown) - SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [sTRAIGHT_JOIN] [sql_SMALL_RESULT] [sql_BIG_RESULT] [sql_BUFFER_RESULT] [sql_CACHE | SQL_NO_CACHE] [sql_CALC_FOUND_ROWS] [i]select_expr[/i] [, [i]select_expr[/i] ...] [FROM [i]table_references[/i] [WHERE [i]where_condition[/i]] [GROUP BY {[i]col_name[/i] | [i]expr[/i] | [i]position[/i]} [ASC | DESC], ... [WITH ROLLUP]] [HAVING [i]where_condition[/i]] [ORDER BY {[i]col_name[/i] | [i]expr[/i] | [i]position[/i]} [ASC | DESC], ...] [LIMIT {[[i]offset[/i],] [i]row_count[/i] | [i]row_count[/i] OFFSET [i]offset[/i]}] [PROCEDURE [i]procedure_name[/i]([i]argument_list[/i])] [iNTO OUTFILE '[i]file_name[/i]' [CHARACTER SET [i]charset_name[/i]] [i]export_options[/i] | INTO DUMPFILE '[i]file_name[/i]' | INTO [i]var_name[/i] [, [i]var_name[/i]]] [FOR UPDATE | LOCK IN SHARE MODE]] Quote Link to comment Share on other sites More sharing options...
krisw44 Posted November 26, 2012 Share Posted November 26, 2012 I just found another issue. You are using the same variable name for the query as you are for the result. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2012 Share Posted November 26, 2012 Reusing a variable, assuming you don't need the original contents of that variable, isn't a problem. Quote Link to comment Share on other sites More sharing options...
designanddev Posted November 26, 2012 Author Share Posted November 26, 2012 well how would you rewrite this? i am very new to php and mysql can you point this out to me? Kind regards Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 27, 2012 Share Posted November 27, 2012 See all the junk PFM Posted? In what order are the words "group by" and "where"? 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.