Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    MySQL Connect with PHP

    In this tutorial, you will learn how to use PHP to connect to a MySQL database and execute queries to retrieve and manipulate data.

    PHP is a popular programming language used in web development to interact with databases. If you want to use PHP to connect to a MySQL database, there are a few steps you need to follow.

    Prerequisites

    Before you begin, make sure you have the following requirements:

    • A running MySQL server
    • A database that you want to connect to
    • PHP installed on your machine

    Establishing a Connection

    To connect to a MySQL database using PHP, you must use the mysqli_connect function. This function takes four parameters: the server hostname, the username, the password, and the database name. For example:

    $conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
    

    The mysqli_connect function will return a connection resource if the connection is successful, or false if the connection fails.

    It's a good idea to check the return value of the mysqli_connect function and handle any errors that may occur. You could do this using the mysqli_connect_error function, which returns the error message if a connection error occurred. For example:

    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    

    Executing Queries

    Once you connect to the MySQL server, you can use the mysqli_query function to execute SQL statements and retrieve data from the database. For example:

    $result = mysqli_query($conn, 'SELECT * FROM users');
    

    The mysqli_query function returns a result set if the query was successful or false if the query failed.

    To retrieve the data from the result set, you can use functions like mysqli_fetch_assoc, which returns an associative array of the current row. You can loop through the result set using a while loop until no more rows are left. For example:

    while ($row = mysqli_fetch_assoc($result)) {
        // Do something with the row data
    }
    

    It's also a good idea to check the return value of the mysqli_query function and handle any possible errors. You can do this using the mysqli_error function, which returns the error message if a query error occurs. For example:

    if (!$result) {
        die("Query failed: " . mysqli_error($conn));
    }
    

    Closing the Connection

    When you're done using the connection to the MySQL server, closing it to free up resources is a good idea. You can do this using the mysqli_close function:

    mysqli_close($conn);
    


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.