0

I just can't get either fetch_all or fetch_assoc working and I have the mysqlnd driver.

 elseif( $_GET["action"] == "purgeuserinvites"){
    $username = $_GET["username"];
    $sql = "SELECT id FROM users WHERE username = ?";
    $testresult = $db->prepare($sql);
    $testresult->bind_param("s", $username);
    $fetched = $testresult->execute();
    $testresult->store_result();
    while($row = $fetched->fetch_all()){ //Also tried fetch_assoc to no avail (SAME ERROR) //LINE 173
        $user_id = $row['id'];
    }
    $testresult->close();
    $sql2 = "SELECT COUNT(*) AS total FROM codes WHERE used = ? AND `by` = ?";
    $isused = 0;
    $result2 = $db->prepare($sql2);
    $result2->bind_param("ss", $isused, $user_id);
    $fetched2 = $result2->execute();
    $result2->store_result();
    while($row2 = $fetched2->fetch_all()){ //Also tried fetch_assoc to no avail (SAME ERROR)
        $total = $row2['total'];
    }
    $result2->close();
    $sql3 = "SELECT * FROM codes WHERE used = ? AND `by` = ?";
    $isused = 0;
    $ifexist = $db->prepare($sql3);
    $ifexist->bind_param("ss", $isused, $user_id);
    $ifexist->execute();
    $ifexist->close();
    if ($ifexist->num_rows > 0){
    $sql4 = "DELETE FROM codes WHERE used = ? AND `by` = ?";
    $isused = 0;
    $result = $db->prepare($sql4);
    $result->bind_param("ss", $isused, $user_id);
    $result->execute();
    $result->close();
    }else{
        die(json_encode(array("status" => 400, "message" => "User does not have any unused invites")));
    }

    if($result){




        die(json_encode(array("status" => 200, "message" => "$totalpurged")));

    } else {

        die(json_encode(array("status" => 400, "message" => "SQL error")));



    }

}

Error log I am getting:

fetch_all

[11-Nov-2020 18:31:56 America/New_York] PHP Fatal error:  Uncaught Error: Call to a member function fetch_all() on boolean in /home/public_html/forums/index.php:173
Stack trace:
#0 {main}
  thrown in /home/public_html/forums/index.php on line 173

fetch_assoc

[11-Nov-2020 18:39:02 America/New_York] PHP Fatal error:  Uncaught Error: Call to a member function fetch_assoc() on boolean in /home/public_html/forums/index.php:173
Stack trace:
#0 {main}
  thrown in /home/public_html/forums/index.php on line 173

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

2

Your code $fetched = $testresult->execute() will store the result of the execution to $fetched, which will either be "true" or "false" (boolean), which is not an object.

Calling a method on an non-object throws an error.

Please try changing this line

 $fetched = $testresult->execute();

to 2 lines (as follows)

$testresult->execute();
$fetched = $testresult->get_result();
Ken Lee
  • 6,985
  • 3
  • 10
  • 29