Warning mysqli_fetch_assoc expects parameter 1 to be mysqli_result null given in

rusdvl

unread,

Aug 25, 2008, 12:28:22 PM8/25/08

to NZ PHP Users Group

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

this is the code:

<?php
require_once('classes/databaseClassi.php');

$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 = '<p>'.$row['feedback'].'<br />'."\n";
$feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
}
?>
<h2>Participant Feedback</h2>
<?php
echo $feedback;
?>

Here is the class:

<?php
class databaseClassi
{
private $mysqli;
public $result;

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 25, 2008, 12:45:11 PM8/25/08

to

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

->

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

rusdvl:

Steve Wake

unread,

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

to

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 26, 2008, 5:30:55 AM8/26/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 26, 2008, 5:45:11 AM8/26/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:

<?php
//connect to MySQL
$connect = mysql_connect("localhost", "username", "password")
or die("Cannot connect, please check your server connection.");

//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 = '<p>'.$row['feedback'].'<br />'."\n";
$feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
}
?>
<h2>Participant Feedback</h2>
<?php
echo $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 26, 2008, 6:17:24 AM8/26/08

to

while($row = mysql_fetch_assoc($result))
{
$feedback = '<p>'.$row['feedback'].'<br />'."\n";

//this will destroy old content inside $feedback

$feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
}

that why only one result displayed.

should do like this:


$feedback = '';
while($row = mysql_fetch_assoc($result))
{
$feedback .= '<p>'.$row['feedback'].'<br />'."\n"; //
there is a dot before =


$feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
}

regards.

anru

unread,

Aug 26, 2008, 6:14:31 AM8/26/08

to

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 26, 2008, 6:46:07 AM8/26/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  = '<p>'.$row['feedback'].'<br />'."\n";
> //this will destroy old content inside $feedback
>                 $feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
>         }
>
> that why only one result displayed.
>
> should do like this:
>
> $feedback = '';
> while($row = mysql_fetch_assoc($result))
>  {
>                $feedback  .= '<p>'.$row['feedback'].'<br />'."\n"; //
> there is a dot before =
>                 $feedback .= '<i>'.$row['participant'].'</i></p>'."\n";
>  }
>
> regards.
>
> anru
>

Andrew McMurtrie

unread,

Aug 26, 2008, 6:51:30 AM8/26/08

to

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 26, 2008, 7:11:41 AM8/26/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 26, 2008, 7:41:11 AM8/26/08

to

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 26, 2008, 8:35:02 AM8/26/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.