gpt4 book ai didi

php - mysql_fetch_array 和数组键

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:45 25 4
gpt4 key购买 nike

此代码有效:

$row = array(5,6,89,97,101);
$found = array();
$post = 89;
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}

而这并没有,可能是在mysql_fetch_array 中做错了什么;

$found = array();
$q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
$rs = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($rs, MYSQL_NUM);
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
print_r($row);
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}

如果帖子超过 1,则不显示任何内容。有人知道解决这个问题的方法吗?

最佳答案

mysql_fetch_array() 获取一行,在您的情况下,它只包含一列。

您应该立即查询结果集中的所有行(我不知道它是如何工作的),或者您应该这样做:

$q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
$rs = mysql_query($q) or die(mysql_error());
$data = array();
while ($row = mysql_fetch_array($rs, MYSQL_NUM)) {
$data[] = $row[0];
}
for($i = 2; $i < count($data) - 2; $i++){ // adjusted boundaries
if($data[$i]==$post){
$found[] = $data[$i-2];
$found[] = $data[$i-1];
$found[] = $data[$i];
$found[] = $data[$i+1];
$found[] = $data[$i+2];
var_dump($found);
}
}

我也调整了边界:如果你对 $i-2$i+2 的范围感兴趣,你只应该从 运行2end-2

关于php - mysql_fetch_array 和数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229778/

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