我是php新手,我正在尝试使用mysql创建一个表。当我运行我编写的php代码时,它会说:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/engelsmj/public_html/Table/CreateTable.php on line 16 Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/engelsmj/public_html/Table/CreateTable.php on line 23 Notice: Undefined variable: sql in /home/engelsmj/public_html/Table/CreateTable.php on line 25 Warning: mysqli_query(): Empty query in /home/engelsmj/public_html/Table/CreateTable.php on line 25
Error creating table:
I believe it is connecting to mysql, but is having an error with actually creating the table. I've worked through my code several times but I can't figure out what's wrong with it and why it won't create my table. Thanks for any help with this.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$con=mysqli_connect("*******","******","*******");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db("*****", $con);
mysqli_query("CREATE TABLE Friends(
idNumber INT NOT NULL AUTO_INCREMENT,
Name varchar(30),
Phone varchar(30),
Age int,
PRIMARY KEY (idNumber))");
if (mysqli_query($con,$sql)) {
echo "Friends table created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
?>
最佳答案
必须切换这些值:
//Wrong
mysqli_select_db("*****", $con);
//Right
mysqli_select_db($con, "*****");
至于查询,您必须添加$sql而不是mysqli_query,因为稍后在if语句中会这样做:
$sql = "CREATE TABLE Friends(
idNumber INT NOT NULL AUTO_INCREMENT,
Name varchar(30),
Phone varchar(30),
Age int,
PRIMARY KEY (idNumber))";
关于php - 尝试使用php创建表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23871019/