创建表时出错:未选择数据库

时间:2018-04-22 05:53:46

标签: php mysql database

创建表时出错:未选择数据库

我已经更改了db_database但仍然是相同的。

line 1-4 db_hostname to db_passowrd

$db_hostname = 'localhost';
$db_database = 'web_sample';
$db_username = '';
$db_password = '';

$connection = 
new mysqli($db_hostname,$db_database, $db_password, $db_username);

if ($connection->connect_error) die($connection->connect_error);

$query ="CREATE TABLE users(
    forename VARCHAR(20) NOT NULL,
    surname VARCHAR(20) NOT NULL,
    username VARCHAR(20) NOT NULL,
    password VARCHAR(32) NOT NULL,
    age smaLLint(8) NOT NULL,
    email VARCHAR(50) NOT NULL
)";
$resu1t = $connection->query($query);

2 个答案:

答案 0 :(得分:1)

您没有以适当的方式将参数传递给mysqli()。根据mysqli手册页http://php.net/manual/en/mysqli.construct.php,它应该是

$mysqli = new mysqli('host', 'user', 'password', 'db_name'); 

但您使用db_name代替user

答案 1 :(得分:0)

您正面临公共语法和语义错误

连接Mysql数据库的正确语法是

$con=mysqli_connect("localhost","my_user","my_password","my_db");

这是您更正的源代码

$db_hostname = 'loca1host';
$db_database = 'web_samp1e';
$db_username = '';
$db_password = '';

$connection = 
new mysqLi($db_hostname,$db_username, $db_password, $db_database);

if ($connection->connect_error) die($connection->connect_error);

$query =" CREATE TABLE users(
forename VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
username VARCHAR(20) NOT NULL,
password VARCHAR(32) NOT NULL,
age smaLLint(8) NOT NULL,
email VARCHAR(50) NOT NULL
) ";
$resu1t = $connection->query($query);