Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Wenn wir eine Datenbank in MySQL mit PHP mit dem folgenden Code erstellen, wo stellen wir die Verbindung her und wo stellen wir die Datenbank her?

Hier ist eine einfache Erklärung, welche Linien was tun. Wenn Sie genau wissen möchten, was die einzelnen Teile davon bedeuten, dann sagen Sie bitte welche, damit sie Ihnen näher erklärt werden können. Oder die richtigen Links, auf die verwiesen wird.

Mir ist aufgefallen, dass Sie W3Schools verwenden beispielsweise als nahezu exaktes Kopieren und Einfügen. Haben Sie MySQL auf Ihrem Rechner installiert und einen Benutzernamen und ein Passwort erstellt?

<?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
?>