简体   繁体   English

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

[英]PHP mysqli_connect not working but mysql_connect is working fine

I'm trying to connect to a mysql database using mysql_connect and it fetches the desired result. 我正在尝试使用mysql_connect连接到mysql数据库,它将获取所需的结果。 However when I'm trying to fetch using mysqli_connect below error msg is shown: 但是,当我尝试使用mysqli_connect读取以下错误消息时,将显示错误消息:

Access Denied for user "@"'localhost ' to database "tbl_name" 访问用户“ @”“ localhost”到数据库“ tbl_name”被拒绝

Here is my Php code: 这是我的PHP代码:

db_connect.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 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);
}
?>

You forgot to comment this line: 您忘记评论此行:

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

That is still trying to connect and is what is throwing your error. 那仍然是尝试连接,并且是引发您的错误的原因。

Also, your close() method needs to be updated to mysqli. 另外,您的close()方法需要更新为mysqli。

It should be: 它应该是:

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

Have a look at the manual and try to use mysqli as objected-oriented as much as possible. 查看手册,并尝试尽可能将mysqli用作面向对象。

i got the answer and here is the updated code:- 我得到了答案,这是更新的代码:-

<?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);
?>      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM