Register.php数据提交时未选择数据库错误


No Database Selected Error on Register.php Data Submit

您好!我已经完成了我的PHP脚本,它允许我让数据库从Register页面表单中插入用户信息。我认为我已经正确地设置了一切,以建立与数据库的连接,但显然出了问题。我在处理Register表单后得到Error: No database selected。这是我的config.php文件,它定义了我的连接信息:

<?php
//Database Information
// DB Username    
define('DB_USER', 'root');
// DB Password
define('DB_PASS', 'TestAccess!');
// DB Name     
define('DB_NAME', 'hooterlove');
?>

即使我有这个设置并传递给mysqli_connect函数,我似乎也无法访问数据库。如果它不存在,我就不需要制作,因为它确实存在。这是hootDB.php文件,但我哪里出错了?

<?php
// Database class
if(!class_exists('HooterLoveDB')){
class HooterLoveDB{
    /**
     * Connects to the database server and selects a database
     *
     * PHP4 compatibility layer for calling the PHP5 constructor.
     *
     * @uses HooterLoveDB::__construct()
     *
     * You cannot use the same name for the function as for the Class name!
     */ 
    function HooterReturn() {
        return $this->__construct();
    }
    /**
     * Connects to the database server and selects a database
     *
     * PHP5 style constructor for compatibility with PHP5. Does
     * the actual setting up of the connection to the database.
     *
     */
    function __construct() {
        $this->connect();
    }
    /**
     * Connect to and select database
     *
     * @uses the constants defined in config.php
     */ 
    function connect() {
        $link = mysqli_connect('localhost', DB_USER, DB_PASS);
        if (!$link) {
            die('Could not connect: ' . mysqli_error());
        }
        $db_selected = mysqli_select_db($link, DB_NAME);
        if (!$db_selected) {
            die('Can''t use ' . DB_NAME . ': ' . mysqli_error($link));
        }
    }
    /**
     * Clean the array using mysql_real_escape_string
     *
     * Cleans an array by array mapping mysql_real_escape_string
     * onto every item in the array.
     *
     * @param array $array The array to be cleaned
     * @return array $array The cleaned array
     */
    /*function clean($array) {
        return array_map(mysqli_real_escape_string(mysqli_connect('localhost', DB_USER, DB_PASS)), $array);
    } */
    function clean($array)
    {
        $link = mysqli_connect('localhost', DB_USER, DB_PASS);
        return array_map(function($value) use ($link)
        {
            mysqli_real_escape_string($link, $value);
        }, $array);
    }
    /**
     * Create a secure hash
     *
     * Creates a secure copy of the user password for storage
     * in the database.
     *
     * @param string $password The user's created password
     * @param string $nonce A user-specific NONCE
     * @return string $secureHash The hashed password
     */
    function hash_password($password, $nonce) {
      $secureHash = hash_hmac('sha512', $password . $nonce, SITE_KEY);
      return $secureHash;
    }
    /**
     * Insert data into the database
     *
     * Does the actual insertion of data into the database.
     *
     * @param resource $link The MySQL Resource link
     * @param string $table The name of the table to insert data into
     * @param array $fields An array of the fields to insert data into
     * @param array $values An array of the values to be inserted
     */
    function insert($link, $table, $fields, $values) {
        $fields = implode(", ", $fields);
        $values = implode("', '", $values);
        $sql="INSERT INTO $table (id, $fields) VALUES ('', '$values')";
        if (!mysqli_query($link = mysqli_connect('localhost', DB_USER, DB_PASS), $sql)) {
            die('Error: ' . mysqli_error($link));
        } else {
            return TRUE;
        }
    }
    /**
     * Select data from the database
     *
     * Grabs the requested data from the database.
     *
     * @param string $table The name of the table to select data from
     * @param string $columns The columns to return
     * @param array $where The field(s) to search a specific value for
     * @param array $equals The value being searched for
     */
    function select($sql) {
        $results = mysqli_query($link = mysqli_connect('localhost', DB_USER, DB_PASS), $sql);
        return $results;
    }
}
}
//Now, instantiate the DB class
$hldb = new HooterLoveDB;
?>

我知道问题可能出在我的function connect()上,但我不知道如何让车轮移动以真正建立连接。有什么想法可以帮助吗?非常感谢。

您的mysqli_connect('localhost'、DB_USER、DB_PASS)没有数据库

应为mysqli_connect('localhost'、DB_USER、DB_PASS、DB_NAME)

http://php.net/manual/en/function.mysqli-connect.php