如何解决表中显示的问题?这是在我尝试从我的数据库中检索数据时发生的。在我的java代码中,我尝试调用的json数组为null。
db_connect.php
<?php
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());
// 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();
}
}
?>'
retEqp.php
<?php
/*
* Following code will list all the products
*/
// 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 facilities_equipments where item_Type='Equipment'") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["equipments"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$equipment = array();
$equipment["item_ID"] = $row["item_ID"];
$equipment["item_Name"] = $row["item_Name"];
// push single product into final response array
array_push($response["equipments"], $equipment);
}
// 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);
}
?>
答案 0 :(得分:0)
替换您的代码
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
带
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
关闭所有已弃用的警告,包括mysql _ *:
error_reporting(E_ALL ^ E_DEPRECATED);