1
我從數據庫中獲取數據並在json中輸出數據,但獲得空的JSON。不知道爲什麼?我得到一個空的數組職位。你可以檢查我的代碼,你能幫我一下嗎?獲取空的JSON響應
這裏是我的代碼
<?php
//Turn off all error reporting
//error_reporting(0);
ini_set ("display_errors", "1");
error_reporting(E_ALL);
define("ENCRYPTION_KEY", "[email protected]#$%^&*");
/**
* Returns an encrypted & utf8-encoded
*/
function encrypt($pure_string, $encryption_key) {
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
return $encrypted_string;
}
/**
* Returns decrypted original string
*/
function decrypt($encrypted_string, $encryption_key) {
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
return $decrypted_string;
}
if(($_GET['action'])&&($_GET['username'])&&($_GET['key'])) {
$select = $_GET['action'];
$username =$_GET['username']; //no default
$key= $_GET['key'];
if($key=='India'){
if($select=='select'){
/* connect to the db */
$connect = mysqli_connect('localhost','root','')or die("Couldn't connect to database!");
mysqli_select_db($connect,'easy_sign') or die ("Couldn't find database");
$query ="SELECT * FROM origin WHERE username ='$username' ";
$result = mysqli_query($connect,$query);
$numrows=mysqli_num_rows($result);
if($numrows!==0)
{
while($row = mysqli_fetch_array($result))
{
$username = $row['username'];
$path = $row['path'];
$decrypted_path = decrypt($path, ENCRYPTION_KEY);
$filename = $row['filename'];
$decrypted_name = decrypt($filename, ENCRYPTION_KEY);
$date = $row['date'];
}
/* create one master array of the records */
$posts = array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
/* output in necessary format */
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
/* disconnect from the db */
@mysqli_close($link);
}
}
}
}
?>
嘗試移除'header('Content-type:application/json');' –
沒有影響好友.. –
你是否在'while($ post = mysqli_fetch_assoc($ result))'中獲得任何數據。查詢在哪裏? –