我有以下php文件来创建数据库和表:
<?php
$slName = $_POST['slName'];
$con=mysqli_connect('localhost', 'setlist', 'music');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE $slName";
if (mysqli_query($con,$sql))
{
echo "Database " . $slName . " created successfully";
}
else
{
echo "Error creating database: " . mysqli_error();
}
// Create table
$tbl="CREATE TABLE main (name TEXT,orderno INT,Age TEXT)";
// Execute query
if (mysqli_query($con,$tbl))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error();
}
?>
运行时我会返回:
数据库创建成功创建表时出错:
我通过命令提示符成功执行了此操作:
CREATE TABLE main (name TEXT,orderno INT,Age TEXT);
我做错了什么?
非常感谢!
洛伦
答案 0 :(得分:2)
您必须选择一个数据库以在
中创建表格 mysqli_select_db($slName);
答案 1 :(得分:1)
创建数据库后,添加mysqli_select_db($slName);