0

I'm new to PHP but here's my code, and I'm getting :

Fatal error: Uncaught Error: Call to a member function bindParam() on boolean

I have tested and test, not sure what is going wrong, some pointers would really help to move on - thanks in advance.

$url_slot = parse_url($str);
$urlArray = explode('/',$url_slot['path']);
$passid = $urlArray['11']; // serial no
$deviceId = $urlArray['8'];
$passtype = $urlArray['10'];
$servername = "host";
$username = "user";
$password = "******";
$dbname = "db";



try {
    // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


    $stmt = $conn->prepare("INSERT INTO Registrations (device_id, pass_id, pass_type) VALUES
(:device_id,:pass_id,:pass_type,:created,:modified)");
    $stmt->bindParam(':device_id',$device_id);
    $stmt->bindParam(':pass_id',$pass_id);
    $stmt->bindParam(':pass_type',$pass_type);


    $device_id = $deviceId;
    $pass_id = $passid;
    $pass_type = $passtype;

    $stmt->execute();
    $conn = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}



try {
     $conn = new mysqli($servername, $username, $password, $dbname);
    $stmt = $conn->prepare("INSERT INTO Devices (push_token) VALUES
(:push_token)");
    $stmt->bindParam(':push_token',$push_token);
    $push_token = $content['pushToken'];
    $stmt->execute();
    $conn = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>

Any help would be great.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Michael
  • 239
  • 3
  • 12

1 Answers1

1

Whenever you get a Fatal error for sending the wrong type to a function, print the arguments you are trying to pass on the guilty line.

Whenever the wrong type in question is a boolean, check if you wrote tests for every function return that could hurt your program if the function had failed, because a variable that contains a boolean when it shouldn't usually does because the function that gave you that value failed and returned false.

In your case, it's even more simple : The error doesn't tell you that you try to pass a boolean to a function that awaits another type, it tells you that you try to call the method bind_param(), which means that you treat a boolean as an object.

$stmt->bindParam(':device_id',$device_id);

Therefore, it is $stmt which is empty.

 $stmt = $conn->prepare("INSERT INTO Registrations (device_id, pass_id, pass_type) VALUES (:device_id,:pass_id,:pass_type,:created,:modified)");

The function that returns you the value you assign to $stmt being that one, I advise you to test if $stmt is different from false right after setting it, and to print the error type and message from $conn if it isn't.

In addition to that, I get the feeling that you aren't yet accustomed to work with API, you seem to lack some experience and are also trying to use PDO exceptions while working with MySQLi. Maybe you should spend some time reading their respective docmuentations.

Stack Overflow is also filled with various questions and docs regarding both.

Sarkouille
  • 1,275
  • 9
  • 16