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

时间:2014-11-04 18:39:43

标签: php mysqli

美好的一天!我已经开发了我的PHP脚本,它允许我从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()上,但我对如何让车轮移动以实际建立连接感到迷茫。任何可能有帮助的想法?谢谢。

1 个答案:

答案 0 :(得分:1)

你的mysqli_connect(&#39; localhost&#39;,DB_USER,DB_PASS)没有数据库

应该是mysqli_connect(&#39; localhost&#39;,DB_USER,DB_PASS,DB_NAME)

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