-1

I have researched it a lot online and spent a lot of time trying to fix this problem. My code

    function createNewAccount() {

        global $response;
        global $conn;

        // prepare and bind
        $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $firstname, $lastname, $email);

        // set parameters and execute
        $firstname = "John";
        $lastname = "Doe";
        $email = "john@example.com";
        $stmt->execute();

}

the error I get is

Warning: mysqli::prepare(): Couldn't fetch mysqli in C:\xampp\htdocs\authentication\register.php on line 105

Fatal error: Uncaught Error: Call to a member function bind_param() on null in C:\xampp\htdocs\authentication\register.php:106 Stack trace: #0 C:\xampp\htdocs\authentication\register.php(139): createNewAccount() #1 {main} thrown in C:\xampp\htdocs\authentication\register.php on line 106

I cant seem to find any solution. Any help is highly appreciated.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Himakar
  • 185
  • 12
  • 1
    PHP, those set parameters need to go before bind, then execute goes after bind – clearshot66 Mar 08 '18 at 15:57
  • 1
    Avoid using `global` and pass any connections/resources around as parameters. – Nigel Ren Mar 08 '18 at 16:00
  • Show how you define `$conn`. And comment out any `conn->close()` you have in your code. – AbraCadaver Mar 08 '18 at 16:34
  • $conn = new mysqli($servername, $username, $password, $dbname); tried commenting conn->close() as well. didnt work – Himakar Mar 08 '18 at 16:41
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Mar 08 '18 at 23:50
  • I have added error reporting as well. This error is strange. – Himakar Mar 09 '18 at 00:23

1 Answers1

-2

PHP Variables need to be set before using them:

function createNewAccount() {

    global $response;   // If you dont need this remove it
    global $conn;

   // set parameters and execute
    $firstname = "John";
    $lastname = "Doe";
    $email = "john@example.com";

    // prepare and bind
    $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $firstname, $lastname, $email);      
    $stmt->execute();

}
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • No they don't. They are passed by reference and created and then changed. – AbraCadaver Mar 08 '18 at 16:26
  • Then your function is broken for another reason because that prepared statement is exactly as it should be. Don't downvote because you didn't supply enough information. Check your $conn because that's the only other thing it could be – clearshot66 Mar 08 '18 at 16:48
  • thats the entire function. $conn is fine with other functions on the page. And i did not downvote anything here. – Himakar Mar 08 '18 at 17:11