1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I am trying to fetch a limited number of results. What am I doing wrong? Thanks.

Discussion in 'PHP' started by Mitchell, Jul 30, 2010.

  1. #1
    Number found:

    Fatal error: Call to a member function fetch_assoc() on a non-object in /home/mysite/public_html/cat_results.php on line 43
    Line 43 is $row = $result->fetch_assoc()


    
    <?php
        include 'conn_mysqli.inc.php';
    
    $query = "SELECT title, price FROM mammals08 ORDER BY created DESC ";
        $result = $db->query( $query );
        $num_results = $result->num_rows;
    ?>
    
     <!DOCTYPE html PUBLIC 
     "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     <title>cat_results</title>
     </head>
     <body>
     <table>
     <?php echo "<p>Number found: ".$num_results."</p>"; 
     
     for ($i = 0; $i <= 6; $i++) {
     $row = $result->fetch_assoc()
     ?>
         <tr>
            <td><?php echo $row['title']; ?></td>
            <td><?php echo $row['price']; ?></td>
        </tr>
    <?php } ?>
    </table>
     </body></html>
    
    PHP:
     
    Mitchell, Jul 30, 2010 IP
  2. po_taka

    po_taka Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    add
    echo $mysqli->error;
    above the "for" to check for errors in query
     
    po_taka, Jul 30, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply.

    I did what you said and tested it. All I get is the same error as mentioned above.

    Fatal error: Call to a member function fetch_assoc() on a non-object in /home/mobielf1/public_html/cat_results.php on line 43
     
    Mitchell, Jul 30, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Why are you using for a loop??, use a while loop and within your query add to the end LIMIT 6 - so it displays only 6

    <?php
      error_reporting(E_ALL);
      $limit = 6;
      $query = "SELECT title, price FROM mammals08 ORDER BY created DESC LIMIT {$limit}";
      $result = mysql_query($query) or trigger_error('Unable to query: '.mysql_error(), E_USER_ERROR);
      $num_results = mysql_num_rows($query);
    ?>
    
     <!DOCTYPE html PUBLIC 
     "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     <title>cat_results</title>
     </head>
     <body>
     <table>
     <?php
      echo "<p>Number found: {$num_results}</p>";
      
      while ($row = mysql_fetch_assoc($result)) :
    ?>
         <tr>
            <td><?php
          echo $row['title'];
    ?></td>
            <td><?php
          echo $row['price'];
    ?></td>
        </tr>
    <?php
    endwhile;
    ?>
    </table>
     </body></html>
    PHP:
     
    Last edited: Jul 30, 2010
    danx10, Jul 30, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I just noticed this is not providing a number found.

    <?php echo "<p>Number found: ".$num_results."</p>";
     
    Mitchell, Jul 30, 2010 IP
  6. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I tried that to earlier and received the same error.

    
     <table>
     <?php echo "<p>Number found: ".$num_results."</p>"; 
     
     //$i = 1;
     //while ($i <= 5) {
     //$row = $result->fetch_assoc();
     //$i++;
     
     echo $mysqli->error;
     for ($i = 0; $i <= 6; $i++) {
     $row = $result->fetch_assoc();
     ?>
     	<tr>
    		<td><?php echo $row['title']; ?></td>
    		<td><?php echo $row['price']; ?></td>
    	</tr>
    <?php } ?>
    </table>
     </body></html>
    
    PHP:
     
    Mitchell, Jul 30, 2010 IP
  7. po_taka

    po_taka Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    it should be
    echo $db->error;
     
    po_taka, Jul 30, 2010 IP
  8. ze0xify

    ze0xify Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hmm.. Well non-object means that MySQL isn't returning data so it's logical to start there.

    Make sure that your query works via PHPMYADMIN:
    SELECT title, price FROM mammals08 ORDER BY created DESC
    Code (markup):
    Also make sure that you have the proper MySQL table selected in your database connection.

    As for your code, I would advise that you do the LIMIT 6; mentioned above as it will improve speed and use less resources. The way you're currently utilizing retrieves all the records, loads them into a PHP resource, then takes only 6 of whatever you retrieved. Using LIMIT will only retrieve the 6 results that you want, which in turn saves resources and has the speed boost as mentioned.

    If the above trials don't work, it may be an issue with your mysql library. If it doesn't work and if you would like assistance from me, feel free to add me on MSN:
     
    ze0xify, Jul 31, 2010 IP
  9. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks people for your help.

    danx10 I'm sorry, I totally glossed over your suggestion to LIMIT 6. I am so new at this I didn't know I could do that. That looks simpler and I will do it that way.

    po_taka I tried echo $db->error; It gave me a meaningful error message: Unknown column 'created' in 'order clause'. When I added ORDER BY created DESC, I did not know I had to do something to the table in my database.

    zeOxify I think your analysis was correct and I will let you know about further assistants , thanks.
     
    Mitchell, Jul 31, 2010 IP