happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

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.


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by