SQL:无法选择数据库

时间:2016-03-17 16:57:56

标签: php sql

当我尝试创建users表时,收到以下错误消息:

  

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

我已尝试SELECTUSE并创建新连接,但我似乎无法连接到我创建的secure_login数据库。也许我一直把它弄错了,我不确定。我知道我没有尝试选择数据库,这是因为我正在做的一切似乎都不起作用。

以下是代码:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected to server successfully" . "</br>";
    // CREATE SECURE LOGIN
    $sql = "CREATE DATABASE IF NOT EXISTS secure_login";
    if ($conn->query($sql) === TRUE) {
        echo "secure_login functional" . "</br>";
        //CREATE USERS TABLE;
        $sql = "CREATE TABLE IF NOT EXISTS users (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            username VARCHAR(30) NOT NULL,
            email VARCHAR(50) NOT NULL,
            password CHAR(128) NOT NULL
        )";
        //MAKE ADMIN USER
        $sql = "INSERT INTO users (id, username, email, password)
        VALUES ('1', 'admin', 'test@test.com', 'password')";
        if ($conn->query($sql) === TRUE) {
            echo "Table users functional" . "</br>";
        } 
        else {
            echo "Error creating table: " . $conn->error . "</br>";
        } 
    } 
    else {
        echo "secure_login not functional: " . $conn->error . "</br>";
    }
?>

4 个答案:

答案 0 :(得分:3)

 $sql = "CREATE DATABASE IF NOT EXISTS secure_login";
    if ($conn->query($sql) === TRUE) {


    // Start Using it at this point :)

您已创建新数据库,但尚未告知mysqli将其用于进一步查询,因此您的SELECT查询没有可供选择的数据库。

你可以简单地做

$conn->select_db("secure_login");

答案 1 :(得分:1)

这是一个工作示例..我刚创建了数据库后创建了一个新连接..使用新创建的数据库。可能还有其他替代方式。但这对我有用..

<?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected to server successfully" . "</br>";
    // CREATE SECURE LOGIN
    $sql = "CREATE DATABASE IF NOT EXISTS secure_login";
    if ($conn->query($sql) === TRUE) {
        echo "secure_login functional" . "</br>";
        $dbname= "secure_login";
        $newConnection = new mysqli($servername, $username, $password,$dbname);
        //CREATE USERS TABLE;
        $sql = "CREATE TABLE IF NOT EXISTS users (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            username VARCHAR(30) NOT NULL,
            email VARCHAR(50) NOT NULL,
            password CHAR(128) NOT NULL
        )";
        if ( $newConnection->query($sql) === TRUE) {
            echo "Table users functional" . "</br>";
        } 
        else {
            echo "Error creating table: " .  $newConnection->error . "</br>";
        } 
        //MAKE ADMIN USER
        $sql = "INSERT INTO users (id, username, email, password)
        VALUES ('1', 'admin', 'test@test.com', 'password')";
        if ( $newConnection->query($sql) === TRUE) {
            echo "inserted successsfully" . "</br>";
        } 
        else {
            echo "Error Inserting: " .  $newConnection->error . "</br>";
        } 
    } 
    else {
        echo "secure_login not functional: " .  $newConnection->error . "</br>";
    }
?>

<强>输出

成功连接服务器

secure_login功能

表用户功能

成功插入

答案 2 :(得分:0)

我相信您只需在创建表格后添加一个select语句。

答案 3 :(得分:0)

您可以在// CREATE USERS TABLE:

之前调用
mysqli_select_db($conn,"secure_login");