如何在不聲明每個(gè)列名的情況下回顯查詢結(jié)果中的所有行(作為 JSON)?即沒(méi)有寫(xiě)作'location_id' => $row['location_id']等等,就像我在下面所做的那樣。<?phprequire_once("./config.php"); //database configuration filerequire_once("./database.php");//database class file$location_id = isset($_GET["location_id"]) ? $_GET["location_id"] : '';$db = new Database();if (isset($_GET["location_id"])){ $sql = "SELECT * FROM location WHERE location_id = $location_id";} else { $sql = "SELECT * FROM location";}$results = $db->conn->query($sql);if($results->num_rows > 0){ $data = array(); while($row = $results->fetch_assoc()) { $data[] = array( 'location_id' => $row['location_id'], 'customer_id' => $row['customer_id'], 'location_id' => $row['location_id'], 'location_name' => $row['location_name'], 'payment_interval' => $row['payment_interval'], 'location_length' => $row['location_length'], 'location_start_date' => $row['location_start_date'], 'location_end_date' => $row['location_end_date'], 'location_status' => $row['location_status'], 'sign_sides' => $row['sign_sides'], 'variable_annual_price' => $row['variable_annual_price'], 'fixed_annual_price' => $row['fixed_annual_price'], 'location_file' => $row['location_file']); }header("Content-Type: application/json; charset=UTF-8");echo json_encode(array('success' => 1, 'result' => $data));} else { echo "Records not found.";}?>更新代碼。現(xiàn)在使用@Dharman 推薦的參數(shù)化準(zhǔn)備語(yǔ)句(謝謝?。?。我Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)在第 17 行。我正在運(yùn)行 PHP 7.3 版。怎么了?我應(yīng)該如何回顯 $data 以便它像以前一樣是 JSON 對(duì)象?<?phpheader("Content-Type: application/json; charset=UTF-8");//include required files in the scriptrequire_once("./config.php"); //database configuration filerequire_once("./database.php");//database class file$object_contract_id = isset($_POST["object_contract_id"]) ? $_POST["object_contract_id"] : '';//create the database connection$db = new Database();
1 回答

拉風(fēng)的咖菲貓
TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
fetch_assoc()
已經(jīng)將您的數(shù)據(jù)作為關(guān)聯(lián)數(shù)組返回,因此您不需要重新進(jìn)行關(guān)聯(lián)。
您可以簡(jiǎn)單地將結(jié)果分配給您的數(shù)據(jù)。=>$data[] = $row
有關(guān)如何fetch_assoc()
工作的詳細(xì)說(shuō)明。這是文檔。
- 1 回答
- 0 關(guān)注
- 130 瀏覽
添加回答
舉報(bào)
0/150
提交
取消