用户''@ localhost'访问被拒绝到数据库

时间:2014-04-28 14:27:48

标签: php html mysql

我有Database1和Database2

我的主要项目Database1无法正常工作但是当我使用Database2时,它可以工作。但是,我无法修复Database1的错误。我很好奇为什么Database1不起作用。

这是我的代码。

<?php

            require_once("models/config.php");
            if (!securePage($_SERVER['PHP_SELF'])){die();}

            require_once("models/header.php");

            if(isset($_POST["product_name"]) && isset($_POST["product_description"]) && isset($_POST["product_price"]))
            {
                $sql = "INSERT INTO preapprove (name,descr,price) VALUES ('".$_POST["product_name"]."', '".$_POST["product_description"]."', '".$_POST["product_price"]."')";
                $sql_query = mysql_db_query($db_name, $sql) or die(mysql_error());
                mysql_close();
            }


            echo "
            <div class='container'>
                <div class='col-md-3'>";

            include("left-nav.php");

            echo "
                </div>

                <div id='main'>";

            echo resultBlock($errors,$successes);

            echo "
                <div id='regbox' class='col-md-9'>
                    <form name='addProducts' action='".$_SERVER['PHP_SELF']."' method='post'>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Name</label>
                            </div>
                            <div class='col-md-6'>
                                <input type='text' name='product_name' class='form-control' />
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Description</label>
                            </div>
                            <div class='col-md-6'>
                                <textarea class='form-control' rows='3' name='product_description'></textarea>
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2'>
                                <label>Price</label>
                            </div>
                            <div class='col-md-6'>
                                <input type='text' name='product_price' class='form-control' />
                            </div>
                        </div>
                    </p>
                    <p>
                        <div class='row'>
                            <div class='col-md-2 col-md-offset'>
                            </div>
                            <div class='col-md-4'>
                                <button type='submit' class='submit btn btn-default'>Submit</button>
                            </div>
                        </div>
                    </p>
                    </form>
                </div>
                </div>
            </div>
            <div id='bottom'></div>
            </body>
            </html>";

            ?>

模型/ DB-settings.php文件

<?php
        /*
        UserCake Version: 2.0.2
        http://usercake.com
        */

        //Database Information
        $db_host = "localhost"; //Host address (most likely localhost)
        $db_name = "bookstore"; //Name of Database
        $db_user = "root"; //Name of database user
        $db_pass = "1234"; //Password for database user
        $db_table_prefix = "uc_";

        GLOBAL $errors;
        GLOBAL $successes;

        $errors = array();
        $successes = array();

        /* Create a new mysqli object with database connection parameters */
        $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
        GLOBAL $mysqli;

        if(mysqli_connect_errno()) {
            echo "Connection Failed: " . mysqli_connect_errno();
            exit();
        }

        //Direct to install directory, if it exists
        if(is_dir("install/"))
        {
            header("Location: install/");
            die();

        }

        ?>

模型/ config.php中

<?php
        /*
        UserCake Version: 2.0.2
        http://usercake.com
        */
        require_once("db-settings.php"); //Require DB connection

        //Retrieve settings
        $stmt = $mysqli->prepare("SELECT id, name, value
            FROM ".$db_table_prefix."configuration");   
        $stmt->execute();
        $stmt->bind_result($id, $name, $value);

        while ($stmt->fetch()){
            $settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
        }
        $stmt->close();

        //Set Settings
        $emailActivation = $settings['activation']['value'];
        $mail_templates_dir = "models/mail-templates/";
        $websiteName = $settings['website_name']['value'];
        $websiteUrl = $settings['website_url']['value'];
        $emailAddress = $settings['email']['value'];
        $resend_activation_threshold = $settings['resend_activation_threshold']['value'];
        $emailDate = date('dmy');
        $language = $settings['language']['value'];
        $template = $settings['template']['value'];

        $master_account = -1;

        $default_hooks = array("#WEBSITENAME#","#WEBSITEURL#","#DATE#");
        $default_replace = array($websiteName,$websiteUrl,$emailDate);

        if (!file_exists($language)) {
            $language = "models/languages/en.php";
        }

        if(!isset($language)) $language = "models/languages/en.php";

        //Pages to require
        require_once($language);
        require_once("class.mail.php");
        require_once("class.user.php");
        require_once("class.newuser.php");
        require_once("funcs.php");

        session_start();

        //Global User Object Var
        //loggedInUser can be used globally if constructed
        if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"]))
        {
            $loggedInUser = $_SESSION["userCakeUser"];
        }

        ?>

1 个答案:

答案 0 :(得分:1)

此错误来自Mysql服务器。

access denied for user ''@'localhost' to database

您需要向用户授予权限。

access denied for user @ 'localhost' to database ''

请运行此sql以检查您的数据库用户:
我的例子:

mysql> SELECT Host,User FROM mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | xxx_user  |
| %         | dda_user2 |
| 127.0.0.1 | root             |
| localhost | dddd_user  |
| localhost | asd_user2 |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)