Lỗi mysqli_fetch_assoc expects parameter 1 to be mysqli_result năm 2024

"; while ($row = mysqli_fetch_assoc($select)) { echo "";

} }

function getnumtopics($cat_id, $subcat_id) { include ('dbconn.php'); $select = mysqli_query($con, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id AND ".$subcat_id." = subcategory_id"); return mysqli_num_rows($select); } ?>

and the structure of the database

Lỗi mysqli_fetch_assoc expects parameter 1 to be mysqli_result năm 2024

Lỗi mysqli_fetch_assoc expects parameter 1 to be mysqli_result năm 2024

Edited March 28, 2020 by Ben555

rusdvl

unread,

Aug 24, 2008, 11:28:22 PM8/24/08

to NZ PHP Users Group

Hi i'm having a little trouble displaying content from a database...

this is the code:

$query = "SELECT feedbackID, feedback, participant FROM tbl_feedback ORDER BY desc"; $feed = new databaseClassi(); $feed-> query($query);

while($row = mysqli_fetch_assoc($feed->result)) { $feedback = '

'.$row['feedback'].'
'."\n"; $feedback .= ''.$row['participant'].'

'."\n"; } ?>

Participant Feedback

Here is the class:

function __construct() { $this->mysqli = new mysqli("localhost","username","password","db_test"); }

function query($query) { if($query) { $this->result = $this->mysqli->query($query); } else { trigger_error($query. ' || ' .$mysqli->error); } } }

When I run the script i get a Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in error...

I'm sure I just messed something up and cant see it. If anyone can help i would really appreciate it...

Thanks.

Dmitry Ruban

unread,

Aug 24, 2008, 11:45:11 PM8/24/08

to [email protected]

while($row = mysqli_fetch_assoc($feed->result))

->

while($row = $feed->result->fetch_assoc())

rusdvl:

Steve Wake

unread,

Aug 24, 2008, 11:45:11 PM8/24/08

to [email protected]

Have you checked the return value from your call to mysqli->query() ? If the query fails then you may be getting a boolean false back, which is then being handed to the mysqli_fetch_assoc instead of a valid result set...

I'd move the actual fetch into the class so you wrap the nature of the db more completely...

rusdvl

unread,

Aug 25, 2008, 4:30:55 PM8/25/08

to NZ PHP Users Group

Hey,

I tried it your way and came up with this error: Fatal error: Call to a member function fetch_assoc() on a non-object in

rusdvl

unread,

Aug 25, 2008, 4:45:11 PM8/25/08

to NZ PHP Users Group

Hey,

Sorry im still learning php so im not entirely sure what you mean... I tried removing it all from a class just to make sure it works:

//make sure we’re using the right database mysql_select_db("db_test");

$query = "SELECT feedbackID, feedback, participant FROM tbl_feedback"; $result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {

$feedback = '

'.$row['feedback'].'
'."\n"; $feedback .= ''.$row['participant'].'

'."\n"; } ?>

Participant Feedback

This did display the result, but only one result... I have 13 in the database that it should be displaying...

anru chen

unread,

Aug 25, 2008, 5:17:24 PM8/25/08

to [email protected]

while($row = mysql_fetch_assoc($result)) { $feedback = '

'.$row['feedback'].'
'."\n";

//this will destroy old content inside $feedback

$feedback .= ''.$row['participant'].'

'."\n"; }

that why only one result displayed.

should do like this:

$feedback = ''; while($row = mysql_fetch_assoc($result)) { $feedback .= '

'.$row['feedback'].'
'."\n"; // there is a dot before =

$feedback .= ''.$row['participant'].'

'."\n"; }

regards.

anru

[email protected]

unread,

Aug 25, 2008, 5:14:31 PM8/25/08

to [email protected]

Showing only the last row I would assume? You are re-defining $feedback each time the while() loops. Try something like:

$feedback = ""; while() { $feedback .= "text\n"; $feedback .= "more text\n"; }

That still does solve your original problem though...

HTH.

Nathan. Web Controlled Robotic Arm http://www.kennedytechnology.com

rusdvl

unread,

Aug 25, 2008, 5:46:07 PM8/25/08

to NZ PHP Users Group

Hey, yea thanks that fixed that problem...

But it did not fix the problem I have when its all in a class.

On Aug 26, 9:17 am, "anru chen" wrote: \> while($row = mysql_fetch_assoc($result)) \> { \> $feedback = '

'.$row['feedback'].'
'."\n"; \> //this will destroy old content inside $feedback \> $feedback .= ''.$row['participant'].'

'."\n"; \> } \> \> that why only one result displayed. \> \> should do like this: \> \> $feedback = ''; \> while($row = mysql_fetch_assoc($result)) \> { \> $feedback .= '

'.$row['feedback'].'
'."\n"; // \> there is a dot before = \> $feedback .= ''.$row['participant'].'

'."\n"; \> } \> \> regards. \> \> anru \>

Andrew McMurtrie

unread,

Aug 25, 2008, 5:51:30 PM8/25/08

to [email protected]

Check your query in a DB gui tool or command line, I would have expected to see a column name in your Order By clause rather than just 'dsc', but I could be wrong. You also might want to manually check the DB wrapper error result in case it didn't trigger you class error as debug.

Andrew

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3385 (20080825) __________

The message was checked by ESET NOD32 Antivirus.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3385 (20080825) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

rusdvl

unread,

Aug 25, 2008, 6:11:41 PM8/25/08

to NZ PHP Users Group

lol... what a dumb mistake... oh well all part of the learning experience... thanks, removing the orderby fixed it and it all displays as it should now...

Andrew McMurtrie

unread,

Aug 25, 2008, 6:41:11 PM8/25/08

to [email protected]

Lol it was a typo wasn't it? Couldn't have been a dumb mistake (never admit to that!). It does pose a question about your classes error handling which looks like it is more the issue because you would have wanted it to catch that. It looks to me like it is only catching when your query string is empty, if you want to error check the result in your class then you will have to manually check your DB wrapper error result because it didn't throw an error. Somthing like this might work better: [code] if(!$query) { trigger_error("Empty query string");

} $this->result = $this->mysqli->query($query);

If($this->mysqli->error) { trigger_error($this->mysqli->error); } [/code]

rusdvl

unread,

Aug 25, 2008, 7:35:02 PM8/25/08

to NZ PHP Users Group

Hey admitting your mistakes is the first step to learning :D (but sure it was a typo)

Thanks for the help and the better code... I wrote that class while studying and havent really revised it since... Hopefully I wont encounter the same problem again.

Doing an assignment for uni, and stuck on an error. Ill attach some files to show the problem and any help very much appreciated.

Error and the screen it comes on

Code

$select = mysqli_query($con, "SELECT * FROM categories");

while ($row = mysqli_fetch_assoc($select)) { echo "

"; echo ""; dispsubcategories($row['cat_id']); echo "
".$row['category_title']."
"; }

}

function dispsubcategories($parent_id) { include ('dbconn.php'); $select = mysqli_query($con, "SELECT cat_id, subcat_id, subcategory_title, subcategory_desc FROM categories, subcategories WHERE ($parent_id = categories.cat_id) AND ($parent_id = subcategories.parent_id"); echo "

CategoriesTopics
".$row['subcategory_title']."
"; echo $row['subcategory_desc']."
"; echo "".getnumtopics($parent_id, $row['subcat_id'])."