【问题标题】:Echo rows in mysqli result without declaring column names?在没有声明列名的情况下,在 mysqli 结果中回显行?
【发布时间】:2019-07-20 20:09:51
【问题描述】:

如何在不声明每个列名的情况下回显查询结果中的所有行(作为 JSON)?即不写'location_id' => $row['location_id'] 等等,就像我在下面所做的那样。

<?php

require_once("./config.php"); //database configuration file
require_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.";
}

?>

更新代码。现在使用@Dharman 推荐的参数化预处理语句(谢谢!)。我在第 17 行得到Parse error: syntax error, unexpected '-&gt;' (T_OBJECT_OPERATOR)。我正在运行 PHP 7.3 版。怎么了?我应该如何回显 $data 使其像以前一样是 JSON 对象?

<?php

header("Content-Type: application/json; charset=UTF-8");

//include required files in the script
require_once("./config.php"); //database configuration file
require_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();

if (isset($_POST["object_contract_id"])){
    $sql = "SELECT * FROM object_contract WHERE object_contract_id = ?";
    $stmt = mysqli->prepare($sql);
    $stmt->bind_param("s", $_POST['object_contract_id']);

} else {
    $sql = "SELECT * FROM object_contract";
    $stmt = mysqli->prepare($sql);
}

$stmt->execute();

$data = $stmt->get_result()->fetch_all();

?>

【问题讨论】:

  • 如果我理解正确的话。你可以关注$date[] = $row。如需更多说明,请查看fetch_assoc() 的文档。 - fetch_assoc() 获取结果行作为关联数组。
  • @GentleSama 谢谢!你是说$data(你写了$date)吗?
  • 哦,是的,当然是 $data。
  • @Dharman 非常感谢!即使是 SELECT 语句也需要这样做吗?如果是这样,在这种情况下我将如何重写我的代码?我只能找到 INSERT 语句的示例。
  • 非常感谢您的帮助。您知道有关如何设置 CRUD 的教程的任何好的资源吗?我的问题中的代码是用于获取“位置”。我还有 6 种其他类型的数据需要处理。我想我没有/不需要创建 24 个不同的 php 文件(每种类型的数据和操作一个)?

标签: php json mysqli resultset


【解决方案1】:

fetch_assoc() 已经将您的数据作为关联数组返回,因此您无需重新进行关联。

您可以简单地将结果分配给您的数据。 => $data[] = $row

有关fetch_assoc() 工作原理的详细说明。这是doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2014-04-09
    相关资源
    最近更新 更多