phpmysqldatabasemysqli

How to connect to MySQL database in PHP


I’m new to PHP and trying to create a login form that connects to a MySQL database. I attempted to connect to the database using the mysqli() function, but I keep getting an error:

Fatal error: Uncaught Error: Call to undefined function mysqli() in C:\Users\vg141fy\login\login.php:25 Stack trace: #0 {main} thrown in C:\Users\vg141fy\login\login.php on line 25

My PHP code:

<?php
    if (isset($_POST["submit"])) {
        $servername = "hidden:servername"; // Server info is correct
        $username = "hidden:username";
        $password = "hidden:password";
        $database = "hidden:database";
    
        $conn = mysqli($servername, $username, $password, $database);
    }
?>

Additional Information:

Questions:


Solution

  • Look at the quick start guide.

    If you are using the OO interface, you need to include the new keyword.

    $conn = new mysqli($servername, $username, $password, $database);
    

    If you are using the procedural interface, then you need to use the mysqli_connect function.

    $conn = mysqli_connect($servername, $username, $password, $database);