0

I am trying to convert MySQL to MySQLi. And I cannot figure out why it brakes on

$stmt2->execute();

and returns error:

Call to a member function execute() on a non-object

Any issue or valid implementing of it!?

// SQL condition "WHERE group=''" where `group` is empty (NULL)
$result = "SELECT id, name FROM table WHERE group='' ORDER BY array ASC";

if ($stmt = $mysqli->prepare($result)) {
    $stmt->execute();
    $stmt->bind_result($id, $name);

    while ($stmt->fetch()) {
        // SQL condition "WHERE group='$id'" where $id defined in $stmt->bind_result($id, $name);
        $result2 = "SELECT name FROM table WHERE group='$id' ORDER BY array ASC";

        $stmt2 = $mysqli->prepare($result2);
        //$valid_stmt2 = $stmt2 === FALSE ? false : true;

        echo $name . "\n";

        //if ($valid_stmt2) {
            // Error cased on $stmt2->execute();
            $stmt2->execute();
            $stmt2->bind_result($name2);

            while ($stmt2->fetch()) {
                echo 'related to: ' . $name2 . "\n";
            }

            $stmt2->close();
        //}
    }

    $stmt->free_result();
    $stmt->close();
}

This question might be related to Possible to use multiple/nested MySQLi statements? Unfortunately I did not find it helpful since it does not provide a valid example or resource for issue.

Update: Simplified code example with comments.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Binyamin
  • 7,493
  • 10
  • 60
  • 82
  • @hakre It is not an duplication of http://stackoverflow.com/questions/8999691/call-to-a-member-function-execute-on-a-non-object. I change `$stmt2 = $mysqli->prepare("SELECT url, name FROM links WHERE group='?' ORDER BY array ASC");` and added `$stmt2->bind_param("i", $id);`, but it still does not work. Anyone could provide a working example? The second `while ()` must return query results based on its condition - an `$id` from first `while ()`. – Binyamin Jun 15 '12 at 12:36
  • Ridiculous how community sees this question as exact duplicate to http://stackoverflow.com/questions/8999691/call-to-a-member-function-execute-on-a-non-object. Requested re-check. – Binyamin Jun 16 '12 at 20:45

1 Answers1

0

You first did

"SELECT id, url, name FROM links WHERE group='' ORDER BY array ASC"

Then you want to use the $id to do

"SELECT url, name FROM links WHERE group='$id' ORDER BY array ASC"

How would get result for that? If group equal empty string, then it will not equal $id(only if $id is empty string too but that's non sense.)

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • `echo "$id $name";` will return `$id` value. What is wrong to return it in `"SELECT url, name FROM links WHERE group='$id' ORDER BY array ASC"`? Any issue? – Binyamin Jun 15 '12 at 01:53
  • @Binyamin Just your logic is wired, `group` won't be `''` and `'$id'` at same time. – xdazz Jun 15 '12 at 01:57
  • @Binyamin And if your `'$id'` is not valid value will cause your sql to be wrong, please use placeholder instead. – xdazz Jun 15 '12 at 02:00
  • If you fallow the code, the first query has condition where `group` is empty. The second query condition is `WHERE group='$id'` `$id` got from the first SQL query results. How you would return valid '$id'? – Binyamin Jun 15 '12 at 12:41