我试图连接到我的数据库,并通过PHP创建一个表,但没有运气。
我试图在文件$mysqli = new mysqli($host, $user, $passwd, $schema);中连接db_inc_oop.php,然后在web localhost:8080/orders.php中运行,后者是用来创建表的localhost:8080/orders.php。我在端口中放置了8080,因为我使用的是XAMPP,并且配置的端口是8080。
这是db_inc_oop.php文件:
<?php
/* Host name of the MySQL server */
$host = 'localhost';
/* MySQL account username */
$user = 'myUser';
/* MySQL account password */
$passwd = 'kpesao123';
/* The schema you want to use */
$schema = 'mySchema';
/* Connection with MySQLi, OOP-style */
$mysqli = new mysqli($host, $user, $passwd, $schema);
/* Check if the connection succeeded */
if (!is_null($mysqli->connect_error))
{
echo 'Connection failed<br>';
echo 'Error number: ' . $mysqli->connect_errno . '<br>';
echo 'Error message: ' . $mysqli->connect_error . '<br>';
die();
}
echo 'Successfully connected!<br>';在orders.php文件中,我编写
<?php
/* Include the MySQLi oop-style syntax connection script */
include 'db_inc_oop.php';我希望连接到我的数据库,然后通过PHP创建一个表。
发布于 2019-06-03 05:56:17
在orders.php中
在下面写代码
<?php
include 'db_inc_oop.php';
$sql = "CREATE TABLE orders (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
order_date TIMESTAMP
)";
if (mysqli_query($mysqli, $sql)) {
echo "Table orders created successfully";
} else {
echo "Error creating table: " . mysqli_error($mysqli);
}尝试一下,它将在您的数据库中创建orders表,但在此之前,请确保您已将create权限授予用于连接数据库的用户。
希望这对你有帮助,这正是你所期望的。
https://stackoverflow.com/questions/56421698
复制相似问题