Problem with mysqli_fetch_assoc()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Luneflin
Forum Newbie
Posts: 3
Joined: Thu Dec 25, 2008 12:54 pm

Problem with mysqli_fetch_assoc()

Post by Luneflin »

Hi. I have a problem with mysqli_fetch_assoc() function (line 20). The full code is:

Code: Select all

<?php
$searchtype=addslashes($_POST['searchtype']);
$searchterm=trim(addslashes($_POST['searchterm']));
if(!$searchtype || !$searchterm)
{
echo "<b><h2>Wrong search conditions entered. Please go back and try again.</h2></b>";
}
@$db=new mysqli("localhost", "bookorama", "bookorama123", "books");
if(mysqli_connect_errno())
{
echo 'Connection to database failed. Please try again later.';
exit;
}
$query="select * from books where ".$searchtype." like '%".$searchterm."%'";
$result=$db->query($query);
$num_results=$result->num_rows;
echo "<font color='green' size=4>Number of books found: ".$num_results.". </font><br/><br/>";
for($i=1; $i<=$num_results; $i++)
{
$row=$result->fetch_assoc();
echo "<b>".$i.". Title: ".htmlspecialchars(stripslashes($row['title'])); 
echo "</b><br/>Author: ".stripslashes($row['author']);
echo "<br/>ISBN: ".stripslashes($row['isbn']);
echo "<br/>Price: £".stripslashes($row['price']);
echo "<br/><br/>";
}
$result->free();
$db->close();
?>
The error message says something like: 'Apache HTTP server has encountered a problem and needs to close. .. Connection Interrupted. The document contains no data. The network link was interrupted while negotiating a connection. Please try again.'
However, when I replace fetch_assoc with fetch_row, everything works just fine.
PHP version 5.2.8, Apache 2.2.11, MySQL 5.1.
Corresponding Apache error log entries:
[Fri Dec 26 07:45:53 2008] [notice] Parent: child process exited with status 3221225477 -- Restarting.
[Fri Dec 26 07:45:55 2008] [notice] Apache/2.2.11 (Win32) PHP/5.2.8 mod_ssl/2.2.11 OpenSSL/0.9.8i configured -- resuming normal operations
[Fri Dec 26 07:45:55 2008] [notice] Server built: Dec 10 2008 00:10:06
[Fri Dec 26 07:45:55 2008] [notice] Parent: Created child process 6116
[Fri Dec 26 07:45:55 2008] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Fri Dec 26 07:45:56 2008] [notice] Child 6116: Child process is running
[Fri Dec 26 07:45:56 2008] [notice] Child 6116: Acquired the start mutex.
[Fri Dec 26 07:45:56 2008] [notice] Child 6116: Starting 64 worker threads.
[Fri Dec 26 07:45:56 2008] [notice] Child 6116: Starting thread to listen on port 80.

Does anyone have any idea what causes this problem?
Last edited by Luneflin on Sun Dec 28, 2008 7:15 am, edited 3 times in total.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: Problem with mysqli_fetch_assoc()

Post by cptnwinky »

I think it might be because you have the $result->fetch_assoc() function inside your loop. In other words, if the number of times your looping is a lot then your sending many many requests to the sql server at once. Try this...

Code: Select all

 
foreach($result->fetch_assoc() as $row)
{
echo "<b>".$i.". Title: ".htmlspecialchars(stripslashes($row['title']));
echo "</b><br/>Author: ".stripslashes($row['author']);
echo "<br/>ISBN: ".stripslashes($row['isbn']);
echo "<br/>Price: £".stripslashes($row['price']);
echo "<br/><br/>";
}
 
Luneflin
Forum Newbie
Posts: 3
Joined: Thu Dec 25, 2008 12:54 pm

Re: Problem with mysqli_fetch_assoc()

Post by Luneflin »

Hi. Thanks for your response. The code you supplied doesn't work either. Nor does fetch_object version. Only fetch_row does. Number of iterations of 'for' loop shouldn't be an issue, as this is a very small database. Also, if it were about too many requests to mysql server, I guess fetch_row should produce similar error?
-----
I tried to run the code with fetch_assoc outside of any loop and got the same error.
Luneflin
Forum Newbie
Posts: 3
Joined: Thu Dec 25, 2008 12:54 pm

Re: Problem with mysqli_fetch_assoc()

Post by Luneflin »

I uninstalled php, mysql and apache and instaled xampp instead. And everything works fine.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Problem with mysqli_fetch_assoc()

Post by VladSun »

cptnwinky wrote:I think it might be because you have the $result->fetch_assoc() function inside your loop. In other words, if the number of times your looping is a lot then your sending many many requests to the sql server at once. Try this...

Code: Select all

$result=$db->query($query);
$num_results=$result->num_rows;
for($i=1; $i<=$num_results; $i++)
{
    $row=$result->fetch_assoc();
    // process $row
}

Code: Select all

$result=$db->query($query);
for($i=1 ;$row=$result->fetch_assoc(); $i++)
{
    // process $row
}

Code: Select all

$result=$db->query($query);
while($row=$result->fetch_assoc())
{
    // process $row
}

Code: Select all

$result=$db->query($query);
foreach($result->fetch_assoc() as $row)
{
    // process $row
}
All of these are correct in every aspect.
There is no real difference between your and Luneflin's code.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply