happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

Connecting to MySQL with PHP

Learn how to integrate PHP with MySQL. The function used to connect to MySQL from PHP uses a function called mysqli. Usage is as follows.

<?php
    $host = 'hostname';
    $user = 'account';
    $pw = 'password';
    $dbName = 'database name';
    $mysqli = new mysqli($host, $user, $pw, $dbName);
?>

Enter the host, account name, password, and database name to use as arguments to mysqli.
So let's actually use mysqli in the editor.
Write and run the code as shown below.

<?php
    $host = 'localhost';
    $user = 'root';
    $pw = 'root';
    $dbName = 'myClass';
    $mysqli = new mysqli($host, $user, $pw, $dbName);

    if($mysqli){
        echo "MySQL connection successful";
    }else{
        echo "MySQL connection failed";
    }
?>

After writing as above, save the file as [connect.php] in the htdocs folder,
run the web browser and enter localhost / connect.php to see the result.

PHP MYSQL

Now that you know how to connect to MySQL with PHP, let's see it in the next tutorial to do a variety of things.

강좌로 돌아가기