Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环MySQL结果_Php_Mysql - Fatal编程技术网

Php 循环MySQL结果

Php 循环MySQL结果,php,mysql,Php,Mysql,我已经有一段时间没有使用PHP和MySQL了,所以很不幸我失去了一些知识,我需要一些代码方面的帮助 下面我有一些代码,可以从DB中获取9个不同的值,但它对我不起作用。我无法判断正确与否,因为PHP错误消息设置为off 是我错了还是这里出了什么问题 for (i = 0; i < 10; i ++) { $query2 = "SELECT * from arkitekturobjekt WHERE id = '{$buildingIdArray[i]}'"; $result2 = $mysq

我已经有一段时间没有使用PHP和MySQL了,所以很不幸我失去了一些知识,我需要一些代码方面的帮助

下面我有一些代码,可以从DB中获取9个不同的值,但它对我不起作用。我无法判断正确与否,因为PHP错误消息设置为off

是我错了还是这里出了什么问题

for (i = 0; i < 10; i ++) {
$query2 = "SELECT * from arkitekturobjekt WHERE id = '{$buildingIdArray[i]}'";
$result2 = $mysqli->query($query2);
$row = mysqli_fetch_object($result2);
// Do some stuff here
}
如果我这样做,它会起作用:

$buildingIdArray  = array(1,2,3,4,5,6,7,8,9);

有什么问题吗?

$i
不是
i

for ($i = 0; $i < 10; $i ++) 
($i=0;$i<10;$i++)的
您不应该在for循环中运行查询

使用循环构建查询,然后只运行一个查询

// prepare the where conditions
$where2 = Array();
for($i = 0; $i < 10; $i ++) {
  $where2[] = "`id` = '" . $buildingIdArray[$i] . "'";
}

// put the query and the conditions together
$query2 = "SELECT * FROM `arkitekturobjekt` WHERE " . implode(' OR ', $where2) . ";";

// run the query and loop results
$result2 = $mysqli->query($query2);
while($row = mysqli_fetch_object($result2)) {
  // do stuff with the results here
}
//准备where条件
$where2=数组();
对于($i=0;$i<10;$i++){
$where2[]=“`id`=””$buildingIdArray[$i]。“”;
}
//将查询和条件放在一起
$query2=“从'arkitekturobjekt`WHERE'中选择*”。内爆('OR',$where2)。";";
//运行查询并循环结果
$result2=$mysqli->query($query2);
while($row=mysqli\u fetch\u对象($result2)){
//在这里处理结果
}

这就是我的评论的意思:“您确定要在循环中查询,而不仅仅是组成一个id列表,查询一次,然后在结果中循环?例如:WHERE id in(1,2,4,6,10)”


记住,你仍然要对这里的卫生处理负责。我不知道是什么填充了你的建筑id数组,所以如果它来自$\u-GET、$\u-POST、$\u-REQUEST,或者其他任何可以由用户生成的东西,那么你可以打开mysql注入,而($row=mysql\u-fetch\u-object($result2)){…}在开发时打开errormessage,测试代码
错误报告(E\u-ALL)您确定要在循环中查询,而不仅仅是组成一个id列表,查询一次,然后循环查询结果吗?示例:id在(1,2,4,6,10)中的位置@KaiQing你能用一个简单的代码示例来回答吗?它帮助很大!我最近几周一直在用Java编程,就是这样,我忘记了$sign。
// prepare the where conditions
$where2 = Array();
for($i = 0; $i < 10; $i ++) {
  $where2[] = "`id` = '" . $buildingIdArray[$i] . "'";
}

// put the query and the conditions together
$query2 = "SELECT * FROM `arkitekturobjekt` WHERE " . implode(' OR ', $where2) . ";";

// run the query and loop results
$result2 = $mysqli->query($query2);
while($row = mysqli_fetch_object($result2)) {
  // do stuff with the results here
}
// get the first 10 items from building array
$id_arr = array_slice($buildingIdArray, 0, 10); 

$query2 = "SELECT * from arkitekturobjekt WHERE id IN (". implode(',', $id_arr) .")";
$result2 = $mysqli->query($query2);

while($row = mysqli_fetch_object($result2))
{
  //Do some stuff here
}