sql >> Base de Datos >  >> RDS >> Mysql

Al crear una base de datos en MySQL con PHP usando el siguiente código ¿dónde hacemos la conexión y dónde hacemos la base de datos?

Aquí hay una explicación simple de qué líneas hacen qué. Si desea saber específicamente qué significan las partes individuales de estos, indique cuáles para que se le puedan explicar mejor. O los enlaces correctos señalados.

Veo que está utilizando W3Schools ejemplo, como un copiar y pegar casi exacto. ¿Ha instalado MySQL en su máquina y ha creado un nombre de usuario y una contraseña?

<?php
    $servername = "localhost"; // This is the location of your server running MySQL
    $username = "username"; // This is the username for MySQL
    $password = "password"; // This is the password for MySQL

    // Create connection
    $conn = new mysqli($servername, $username, $password); // This is where you create a connection

    // Check connection
    if ($conn->connect_error) { // This checks if the connection happened
        die("Connection failed: " . $conn->connect_error); // and produces an error message if not
    }  // otherwise we move on

    // Create database
    $sql = "CREATE DATABASE myDB"; // This is the SQL query which is sent to the MySQL server
    if ($conn->query($sql) === TRUE) { // When the if statement begins here, it executes the query and test if it returns true
        echo "Database created successfully"; // If it returns true then here is the message is returns
    }
    else {
        echo "Error creating database: " . $conn->error; // Or if there was error with the query this is returned
    }

    $conn->close(); // Close the connection when it is no longer in use
?>