在MySQL中使用以下代码在PHP中创建数据库时,我们在哪里创建连接,我们在哪里做数据库? [英] 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?

查看:115
本文介绍了在MySQL中使用以下代码在PHP中创建数据库时,我们在哪里创建连接,我们在哪里做数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,需要一些解释。这里是一个代码,我们用PHP连接到MySQL。你能解释我在哪里的声明,使连接?我只能看到我们定义$ conn的值是什么,但它是否意味着执行呢?另一件事是:我们在哪里创建数据库?我可以看到,我们给字符串CREATE DATABASE myDB作为值到$ sql和我们有一个if语句,但表达式($ conn-> query($ sql)=== TRUE)也评估?对我来说奇怪,有人可以向我解释一下吗? :)谢谢!

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();
?>


推荐答案

这里是一个简单的解释哪些行做什么。如果你想知道这些的具体部分是什么意思,那么请说明哪些,以便他们可以进一步解释给你。或指向的正确链接。

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.

我注意到您正在使用 W3Schools 示例,几乎完全复制和粘贴。您是否在您的计算机上安装了MySQL并创建了用户名和密码?

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

这篇关于在MySQL中使用以下代码在PHP中创建数据库时,我们在哪里创建连接,我们在哪里做数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆