Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

When creating a database in MySQL with PHP using the following code where does we make the connection and where does we make the database?

I am new in PHP and would need some explanation. Here is a code where we connect to MySQL with PHP. Can you please explain me where is the statement that makes the connection? I can see only that we define what the value of $conn is, but does it mean execution as well? The other thing is: where do we create the database? I can see that we give the string "CREATE DATABASE myDB" as a value to $sql and we have an if statement, but does the expression ($conn->query($sql) === TRUE) also evaluated? It is strange for me, can somebody please explain it to me?! :) Thanks!

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}

$conn->close();
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a simple explanation of which lines do what. If you would like to know specifically what the individual parts of these mean, then please say which ones so they can be further explained to you. Or the correct links pointed to.

I notice that you are using the W3Schools example, as an almost exact copy and paste. Have you installed MySQL on your machine and created a username and password?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...