I have the following php file to create a database and table:
<?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();
}
?>
When it runs I it returns this:
Database created successfully Error creating table:
I executed this via the command prompt with success:
CREATE TABLE main (name TEXT,orderno INT,Age TEXT);
What am I doing wrong?
Thanks so much!
Loren
您必须选择要在其中创建表的数据库
mysqli_select_db($slName);
创建数据库后添加mysqli_select_db($slName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.