-1

I have search stackoverflow about this issue. Even though looks like there are many questions about the topic, non of them solve my problem. I'm having a fetch_assoc() non-object error. I'm utilizing MySQLi and PHP 5.2. I don't have the option to change that. Here is the php code that I wrote that is getting me the error.

<?php
include '_includes/db.php';

$faculty_id = trim($_GET['faculty_id']);

$message = '';
include '_includes/connection.php';
if ($db->connect_error) {
    $message = $db->connect_error;
} else {
    $sql = "SELECT * FROM faculty WHERE faculty_id = $faculty_id";
    $result = $db->query($sql);
    $row = $result->fetch_assoc();
    if ($db->error) {
        $message = $db->error;
    }
}

Any idea why this is happening?

Aryeh Beitz
  • 1,788
  • 20
  • 22
redeemefy
  • 3,891
  • 6
  • 31
  • 49
  • 1
    For the same exact reasons it's happening in the related questions you stated that you already found on StackOverflow. The reason for the error doesn't change. If `$result` is not an object, PHP can not possibly call a method on it. You need to check that `$db->query` __actually__ returns an object first since [it will return `false` when the query fails](http://php.net/mysqli-query). `false` is __not an object__. You cannot call a method on something that is __not an object__. – Sherif Aug 26 '16 at 21:21
  • Your query failed. You failed to check for failure until AFTER you tried using the boolean false returned by your failure, triggering other failures. And on top of that you're vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B Aug 26 '16 at 21:23
  • I have this very code running in other application, in the same server, but different tables since is other application. Your [link](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) explains that the reason of the error is that I have invalid syntax in the query and I don't see where is the error. – redeemefy Aug 26 '16 at 21:36
  • And that's why you HAVE to check for errors properly. Even if your SQL is 100% syntactically correct, it can STILL fail for any number of OTHER reasons. `$result = $db->query(...) or die(mysqli_error($db))`. never assume success with external resources, particularly databases. – Marc B Aug 26 '16 at 21:50

1 Answers1

-2

As the comments have stated, the line:

 $result = $db->query($sql)

Is failing. Which means the query is failing. So $result is getting assigned false.

Wrap that line in an if statement like this and see what it outputs:

if ($result = $db->query($sql)) {
  echo "Success";
} else {
  echo "Failure";
}

If it outputs failure then make sure that all of your database field names are correct and that you have a valid database connection.

Timothy Fisher
  • 911
  • 7
  • 24
  • What if it's not database field names and the connection is good? – Your Common Sense Aug 27 '16 at 04:26
  • I'm still working on this problem. I have two different applications running from the same database but pulling data from two different tables. Is the same code, I just change tables and fields in my code. Once is working perfect and the other is not. I include this code to debugg `if ($result = $db->query($sql)) { echo "Success"; } else { echo "Failure"; }` but is returning a blank page. Home page is pulling the data just fine. I just can't get to Edit or Delete pages. I have looking this code for 9 hours now and can't figure out what is going on. – redeemefy Aug 27 '16 at 04:45
  • The issue isn't with the code then. There's gotta be something wrong with your configuration if you're not getting anything back from that. Is there anything else outputting correctly on that page? – Timothy Fisher Aug 27 '16 at 18:21
  • I finally found the error. `$faculty_id = trim($_GET['faculty_id']);` instead of faculty_id should be jsut `$faculty_id = trim($_GET['id']);` – redeemefy Aug 28 '16 at 04:08
  • glad you figured it out! @gilbert – Timothy Fisher Aug 28 '16 at 19:40