peter_anderson Posted July 6, 2009 Share Posted July 6, 2009 Hi all, I'm getting the following error, but I cannot see why: ERROR: Fatal error: Call to a member function fetch_assoc() on a non-object in {filepath} on line 36 CODE: <?php ini_set('display_errors',1); error_reporting(E_ALL); require_once("aconfig.php"); $eid = $_GET["event"]; echo '<html> <head> <title>Admin Control Panel :: PRINT TICKETS</title> <style type="text/css"> body { font-family: Helvetica, Arial; } </style> </head> <body>'; //start classes $db = new db(); //connect to DB //attempt it $sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']); //check for ERR if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // if($eid == "edit"){ echo '<h1>SELECT EVENT</h1> <p>Select an event to print tickets for</p> <p>'; $query = 'SELECT * from `tickets` ORDER by id'; $query = $sql->real_escape_string($query); // Perform Query $result = $sql->query($query); // Loop Through Result while ($row = $result->fetch_assoc()) { echo '− <a href="ptickets.php?event='.$row[id].'">'.$row[event_name].'</a> <br />'; } echo '</p>'; }else { $query2 = 'SELECT * from `orders` WHERE `id` = '.$eid.', paid = "1"'; $query2 = $sql->real_escape_string($query2); // Perform Query $result2 = $sql->query($query2); // Loop Through Result while ($row2 = $result2->fetch_assoc()) { echo '<table cellspacing="1" cellpadding="1" border="1" width="400"> <tbody> <tr> <td colspan="2"><strong>Online Ticket</strong></td> </tr> <tr> <td width="300"> <p>Event: <strong>'.$row2[eventname].'</strong></p> <p>Holder Name: <strong>'.$row2[name].'</strong></p> <p>No. Adult tickets: <strong>'.$row2[adult_tix].'</strong><br /> No. Concession tickets: <strong>'.$row2[concess_tix].'</strong></p> </td> <td> <p><u>STAFF</u></p> <p><em>Rip this side off once ticket holder has entered.</em></p> </td> </tr> </tbody> </table> <p> </p>'; } } echo '</body></html>'; ?> What the script does: if the page is ?event=edit, it lists all the available events. If not, it checks what event id is at ?event and lists that ID from the DB. What's wrong with it? Thanks Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/ Share on other sites More sharing options...
Maq Posted July 6, 2009 Share Posted July 6, 2009 I think this line should be: while ($row = $sql->fetch_assoc($result)) { $sql is your object, which is an instance of the class db. $result is the resource id that was returned from: $result = $sql->query($query); It doesn't make sense to invoke a method from $result. Instead you should give the resource id ($result) to the fetch_array() method which comes from the 'db' class. Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-869697 Share on other sites More sharing options...
peter_anderson Posted July 6, 2009 Author Share Posted July 6, 2009 I think this line should be: while ($row = $sql->fetch_assoc($result)) { $sql is your object, which is an instance of the class db. $result is the resource id that was returned from: $result = $sql->query($query); It doesn't make sense to invoke a method from $result. Instead you should give the resource id ($result) to the fetch_array() method which comes from the 'db' class. Thanks, It now gives the following error: Fatal error: Call to undefined method mysqli::fetch_assoc() in {filepath} on line 54 Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-869702 Share on other sites More sharing options...
peter_anderson Posted July 6, 2009 Author Share Posted July 6, 2009 Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-869771 Share on other sites More sharing options...
Eggzorcist Posted May 19, 2011 Share Posted May 19, 2011 I'm also having this issue after the doing that solution anyone knows? Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-1217626 Share on other sites More sharing options...
Maq Posted May 19, 2011 Share Posted May 19, 2011 I'm also having this issue after the doing that solution anyone knows? Please start your own thread, this one is nearly 2 years old. Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-1217628 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2011 Share Posted May 19, 2011 A) Please start your own thread for your problem, B) The error means what it says, what ever variable you are using in the $result->fetch_assoc() statement isn't an object because your query failed and returned a false value instead of a result object or you overwrote the variable somewhere else in your code. Link to comment https://forums.phpfreaks.com/topic/164929-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object/#findComment-1217630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.