-1

I am pretty new to using php and I am currently working on a site that has an interactive animal glossary.

I have been editing php pages written by another developer and I have come across an error when I run the bind_param() function.

Here is the meat of the page, a good deal has been removed so that I only expose my error.

if (isset($_GET['id'])) {

    $results = array();

    $stmt = $db_conn->prepare("SELECT specific.id, specific.name, specific.description, specific.group_id, m.id specific_media_id, m.type specific_media_type, m.url specific_media_url FROM ANIMAL_SPECIFIC specific LEFT JOIN MEDIA m on m.id = specific.media_id WHERE GROUP_ID = ?");
    $stmt->bind_param('i', $_GET['id']);
    $stmt->execute();
    $stmt->bind_result($specific_id, $name, $description, $group_id, $specific_media_id, $specific_media_type, $specific_media_url);
    while ($stmt->fetch()) {
        $specific = array(
            "id" => $specific_id,
            "name" => $name,
            "description" => $description,
            "group_id" => $group_id
        );

        if (!empty($specific_media_id)) {
            $media = array(
                "id" => $specific_media_id,
                "type" => $specific_media_type,
                "url" => $specific_media_url
            );
            $specific["media"] = $media;
        } 

        $results["results"]["animal_specific"][] = $specific;
    }
    $stmt->close();

    echo json_encode($results);
}

}

The error I get is: Fatal error: Call to a member function bind_param() on a non-object in /web/animaldetailsJSON.php on line 13 Call Stack: 0.0058 647448 1. {main}() /web/animaldetailsJSON.php:0

I am sure there is some small error that I am overlooking. All I need is a JSON of all SPECIFIC_ANIMALS with a certain group_id

UserK
  • 17
  • 1
  • 6

1 Answers1

0

specific seems to be a reserved word in MySQL, so you either have to use another table alias or enquote it with backticks like in

SELECT `specific`.id, `specific`.name, `specific`.description, `specific`.group_id, m.id specific_media_id, m.type specific_media_type, m.url specific_media_url FROM ANIMAL_specific `specific` LEFT JOIN MEDIA m on m.id = `specific`.media_id WHERE GROUP_ID = ?

Because of this, your query fails and $stmt becomes null, triggering the posted error.

BluePsyduck
  • 1,111
  • 9
  • 9