-2

I'm getting an error in my php mysqli file.

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php:20 Stack trace: #0 C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php(33): writeDatabase('dsgd', 'Lorem@lorem.nl', 'gf', 'awesrdtfgh') #1 {main} thrown in C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php on line 20

It doesnt recocnize the prepare statement on line 20, But I dont see anything wrong with it.


    function dbConnect(){
        $servername = "localhost";                                          // Localhost stays untouched
        $username = "dbUser";                                               // Username of the MySQL database user
        $password = "R4b3Eg5Jt4Y9GKqB";                                         // Password of the MySQL database user
        $conn = new mysqli($servername, $username, $password);                  // Create connection
        if ($conn->connect_error) {                                                 // Check connection
            die("Connection failed: " . $conn->connect_error);
        }
        echo "Connected successfully";
        return;
    }

    function writeDatabase($p_sName,$p_sEmailAddress,$p_sSubject,$p_sMailContent){
       $conn = dbConnect();

    // -------------------- prepare and bind --------------------------------
        //$stmt = $conn->prepare("INSERT INTO `driekvandermeulen.tbl_emailcontacts` (`firstname`, `lastname`, `email`) VALUES (?, ?, ?, ?)");
        $stmt = $conn->prepare();
        $stmt->bind_param($p_sName,$p_sEmailAddress,$p_sSubject,$p_sMailContent);

                $stmt->execute(); // Actual save into the database
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

3

You return nothing from your dbConnect() function. So assigning the result from dbConnect() to a variable will yield to null.

$conn = dbConnect(); This line will result in $conn = null;.

You should try returning the connection object from your dbConnect() function:

function dbConnect(){
    $servername = "localhost";                                          // Localhost stays untouched
    $username = "dbUser";                                               // Username of the MySQL database user
    $password = "R4b3Eg5Jt4Y9GKqB";                                         // Password of the MySQL database user
    $conn = new mysqli($servername, $username, $password);                  // Create connection
    if ($conn->connect_error) {                                                 // Check connection
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    return $conn;  // <------------------ Check this
}
Cid
  • 14,968
  • 4
  • 30
  • 45
Fjarlaegur
  • 1,395
  • 1
  • 14
  • 33
  • 1
    Feel free to rephrase the comment I added that points the modification in the function using your own style – Cid Dec 05 '19 at 13:45
  • Good one on pointing out the corrected line. Will do this in advance. I will leave it like this. – Fjarlaegur Dec 05 '19 at 13:46
  • Using this function will result in "Too many connections" error. It puts the answer under the "Fixing one issue, creating five" category. – Your Common Sense Dec 05 '19 at 14:25
  • 1
    Well, i am not "creating" new issues. It is just the following one in the chain. It is not that my answer will be creating new issues. If my answer would be actually giving him the `dbConnect()` function THEN i would be creating an issue. I merely fixed his syntactical issue. Of course i agree the way it is written is the optimal solution. But that is out of the scope of this question. Although it can be recommended to the person asking the question. – Fjarlaegur Dec 05 '19 at 14:40