Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当使用以下代码在MySQL和PHP中创建数据库时,我们在哪里建立连接,在哪里建立数据库?_Php_Mysql_Database - Fatal编程技术网

当使用以下代码在MySQL和PHP中创建数据库时,我们在哪里建立连接,在哪里建立数据库?

当使用以下代码在MySQL和PHP中创建数据库时,我们在哪里建立连接,在哪里建立数据库?,php,mysql,database,Php,Mysql,Database,我是PHP新手,需要一些解释。下面是我们用PHP连接MySQL的代码。你能解释一下,连接的语句在哪里吗?我只能看到我们定义了$conn的值,但它也意味着执行吗?另一件事是:我们在哪里创建数据库?我可以看到,我们将字符串“createdatabasemydb”作为$sql的一个值,并且我们有一个if语句,但是表达式($conn->query($sql)==TRUE)也进行了计算吗?这对我来说很奇怪,有人能给我解释一下吗?!:)谢谢 下面是一个简单的解释,说明哪些行可以做什么。如果您想具体了解这些部

我是PHP新手,需要一些解释。下面是我们用PHP连接MySQL的代码。你能解释一下,连接的语句在哪里吗?我只能看到我们定义了$conn的值,但它也意味着执行吗?另一件事是:我们在哪里创建数据库?我可以看到,我们将字符串“createdatabasemydb”作为$sql的一个值,并且我们有一个if语句,但是表达式($conn->query($sql)==TRUE)也进行了计算吗?这对我来说很奇怪,有人能给我解释一下吗?!:)谢谢


下面是一个简单的解释,说明哪些行可以做什么。如果您想具体了解这些部分的具体含义,请说明哪些部分,以便进一步向您解释。或指向的正确链接

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

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

虽然您的问题不属于这里(这里是为了帮助您解决编码问题),但我会给您一些解释

PHP读取并执行每一行。“创建连接”部分使用“新建”对象打开一个新连接,并将其保存为一个变量($conn)

($conn->connect\u error)
使用
connect\u error
属性检查连接是否成功。如果已连接,请继续,否则请通过并出错并停止


如果连接成功,则根据变量($conn)中打开的连接创建数据库。

谢谢您的回答!我只是想把一切都安排好。我已经有网络矩阵了。我也需要安装MySQL吗?不用担心,是的,您需要安装MySQL服务器。您可以在MySQL网站和w3schools上获得这方面的说明
<?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
?>