PHP mysqli_connect无法正常工作,但mysql_connect工作正常

时间:2014-09-16 17:48:02

标签: php mysql mysqli

我尝试使用mysql_connect连接到mysql数据库,并获取所需的结果。但是,当我尝试使用mysqli_connect获取时,会显示错误信息:

  

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

这是我的Php代码:

db_connect.php

<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        //$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
        $con = mysqli_connect(DB_SERVER,DB_USER, DB_PASSWORD,DB_DATABASE);

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}

?>

getallimages.php

<?php

/*
 * Following code will list all the images
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all products from products table
$result = mysql_query("SELECT *FROM images_tbl") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["images_id"] = $row["images_id"];
        $product["images_path"] = $row["images_path"];
        $product["submission_date"] = $row["submission_date"];        

        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

3 个答案:

答案 0 :(得分:4)

你忘了评论这一行:

$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

那仍在尝试连接,这就是你的错误。

此外,您的close()方法需要更新为mysqli。

答案 1 :(得分:1)

应该是:

$con = new mysqli(DB_SERVER,DB_USER, DB_PASSWORD,DB_DATABASE);

Have a look at the manual并尝试尽可能使用mysqli作为面向对象。

答案 2 :(得分:0)

我得到了答案,这是更新后的代码: -

<?php
$mysqli = new mysqli("localhost", "root", "", "test");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$res = $mysqli->query("SELECT * FROM images_tbl");

$rows = array(); 

while ($row = $res->fetch_assoc()) {

    $rows[]=$row;



}
echo json_encode(array('Images'=>$rows));
// CLOSE CONNECTION
    mysqli_close($mysqli);
?>