How to connect to a database using PHP and MySQL

Connecting to a database using PHP MySQLi is essential to many web applications. It provides a secure and easy way of accessing and managing data on the server. The syntax of PHP MySQLi is as follows:

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

The mysqli() constructor with four parameters is used to create a new connection to the MySQL database. Here are the different parameters:

Parameter Description
servername The hostname or IP address of the database server
username The username used to connect to the database
password The password used to connect to the database
dbname The name of the database to connect to

Now that we have established a connection to the database, we can execute various SQL queries. Here are some usage example:

    // Select data
    $sql = "SELECT * FROM users";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "Name: " . $row["name"]. " - Email: " . $row["email"]. "
";
        }
    } else {
        echo "0 results";
    }

    // Insert data
    $sql = "INSERT INTO users (name, email) VALUES ('john', 'john@example.com')";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "
" . $conn->error;
    }

    // Update data
    $sql = "UPDATE users SET name='Doe' WHERE id=1";
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    // Delete data
    $sql = "DELETE FROM users WHERE id=2";
    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }

Here are some technical details to keep in mind when using PHP MySQLi:

Technical Details Description
Procedural vs Object-oriented PHP MySQLi supports both procedural and object-oriented approaches to writing queries
Security PHP MySQLi provides a secure way of accessing and managing data on the server
Error handling PHP MySQLi provides a detailed error message whenever a query fails
Performance PHP MySQLi is faster and more efficient than the older MySQL extension

  Written by: Maria Jensen     23-05-2023     Written in: PHP tutorials

4 Comments

  • Alexis says:

    This tutorial was super helpful! 😄 I’ve always wanted to learn how to connect to a database using PHP and MySQL, and this step-by-step guide made it so easy to understand . I appreciate the clear explanations and code examples that were provided. Now I feel confident in my ability to create dynamic websites with database integration. Thanks a bunch! 🙌

  • Shane says:

    This tutorial was incredibly helpful for me! I’ve always struggled with connecting to a database using PHP and MySQL, but the step-by-step instructions and clear explanations made it so much easier to understand . The code samples provided were really handy too, as they allowed me to see how the concepts were applied in real-life situations. Overall, this tutorial was a game-changer for me and my database-related projects. I can’t wait to apply what I’ve learned here. Thank you so much for sharing this valuable knowledge!

  • Zoey says:

    This article was really helpful! I followed the steps and managed to connect to my database successfully . However, I would have appreciated more detailed explanations for each step, especially for newcomers like me. Additionally, some sample code showcasing how to insert, retrieve, and update data in the database would have been a great addition. Nonetheless, I still found this guide quite valuable in getting me started with PHP and MySQL database connectivity.

  • Caroline says:

    This tutorial was incredibly helpful in guiding me through the process of connecting to a database using PHP and MySQL . I had been struggling with this task for a while, but thanks to the step-by-step instructions and clear explanations provided, I was able to successfully establish a connection. The code examples were especially useful in helping me understand the concepts better. I’m grateful for this resource and look forward to exploring more tutorials on BeginnerTuts!

Leave a Reply

Your email address will not be published. Required fields are marked *