gpt4 book ai didi

php - 为什么查询的答案是[1]?

转载 作者:行者123 更新时间:2023-11-29 07:37:16 24 4
gpt4 key购买 nike

首先,这是我发送查询的函数:

function send_query($sql) {
global $rows;
global $conn;
connect();

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
mysqli_free_result($result);
$conn->close();

return $rows;
}

当我这样使用它时:

$rows = send_query("SELECT views FROM posts WHERE postid = " . $_GET['id']);


foreach( $rows as $row ) {
print_r($row);
echo "<BR>";
}

我得到这个结果...

Array ( [postid] => 1 [username] => jesusfreak [unique] => 3 )
Array ( [views] => 0 )

为什么它把我需要的答案放在数组的[1]而不是[0]中?为什么它要把我没有请求的其他东西放入[0]?

最佳答案

看起来是因为你的

global $rows
is coming in with a result already inside it. You are appending the newly queries rows to that variable.

Try using a different (empty) array instead of $rows:

function send_query($sql) {
global $rows;
global $conn;
connect();

$result = $conn->query($sql);
$sqlresults = array();
while ($row = $result->fetch_assoc()) {
$sqlresults[] = $row;
}
mysqli_free_result($result);
$conn->close();

return $sqlresults;
}

关于php - 为什么查询的答案是[1]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com