MySqli:是否可以创建数据库? [英] MySqli: is it possible to create a database?
本文介绍了MySqli:是否可以创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在筛选MySQLi文档,据我所知,我无法使用PHP和MySQLi创建数据库.这是正确的吗?
I've been sifting through the MySQLi docs, and as far as I can tell, I there's no way to create a database using PHP and MySQLi. Is this correct?
推荐答案
CREATE DATABASE语句用于在MySQL中创建数据库.
The CREATE DATABASE statement is used to create a database in MySQL.
- 语法:
- Syntax:
CREATE DATABASE database_name
要使PHP执行SQL指令,首先必须使用以下命令创建一个mysqli对象: 连接到服务器,然后使用MySQLi类的query()方法.
To get PHP to execute the SQL instructions, first you must create a mysqli object with the connection to the server, then use the query() method of the MySQLi class.
- 语法:
- Syntax:
mysqliObj->query($sql_query)
- mysqliObj-是使用新的mysqli()创建的mysqli对象
- $ sql_query-是带有SQL指令的字符串.此方法将查询或命令发送到MySQL连接,将返回结果对象,如果成功,则返回TRUE.失败则为假.
- mysqliObj - is the mysqli object created with new mysqli()
- $sql_query - is a string with SQL instructions. This method sends a query or command to a MySQL connection, will return a result object, or TRUE on success. FALSE on failure.
下面的示例创建一个名为"tests"的数据库:
The following example creates a database called "tests":
<?php
// connect to the MySQL server
$conn = new mysqli('localhost', 'root', 'pass');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// sql query with CREATE DATABASE
$sql = "CREATE DATABASE `tests` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
// Performs the $sql query on the server to create the database
if ($conn->query($sql) === TRUE) {
echo 'Database "tests" successfully created';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
?>
检查此链接 http://coursesweb.net/php-mysql/php -mysql-using-mysqli
这篇关于MySqli:是否可以创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文