[英]Prepared statement result in query
Converted my normal SQL queries to prepared statements a while back, but now I want to implement a function and I'm not sure how. 我将普通的SQL查询转换为准备好的语句,但是现在我想实现一个函数,但不确定如何实现。 Below is my current code: 下面是我当前的代码:
$stmt = $db->stmt_init();
$query = "pretty long query...";
$stmt->prepare($query);
$stmt->bind_param('ssii', $dienstenpage, $huidigegemeente, $startpoint, $limit);
$stmt->execute();
$stmt->bind_result($prepid, $prephoofdrubriek, $prepplaats, $prepbedrijfsnaam, $prepgemeente, $prepbedrijfsomschrijving, $prepbedrijfsslogan, $prepstraatnaam, $prephuisnummer, $preppostcode, $preptelefoonnummer, $prepfax, $prepemail, $prepwebsite, $prepbedrijfslogo, $prepdubb);
$stmt->store_result();
$numrows = $stmt->num_rows;
while($stmt->fetch()) {
Here appears a nice div with all the data for every result (row)
}
All works fine, no hick-ups there. 一切正常,没有任何问题。 However, for some of the page I want to shuffle the first 20 rows (only the first 20!) of the result. 但是,对于某些页面,我想重新排列结果的前20行(仅前20行!)。 Thanks to the help of others I learned the best way to do that is to put the result in a query and do this: 在其他人的帮助下,我学会了最好的方法是将结果放入查询中并执行以下操作:
$first20 = array_splice($allResults,0,20);
shuffle($first20);
array_splice($allResults,0,0,$first20);
However, I'm not sure how to put the results in array? 但是,我不确定如何将结果放入数组中? I should be able to do that in the while loop ($stmt->fetch), but not sure how? 我应该能够在while循环($ stmt-> fetch)中做到这一点,但不确定如何? Once the results are in an array I can slice it with the code above, put in back in the array and display the results in the new order. 将结果放入数组后,我可以使用上面的代码对其进行切片,放回数组中,并以新的顺序显示结果。 How do I insert the rows in an array? 如何将行插入数组中?
THE ANSWER WAS (COMPLETED CODE): 答案是(完整代码):
$array = array();
while($stmt->fetch()) {
$array[] = array('prepid'=>$prepid, 'prephoofdrubriek'=>$prephoofdrubriek, 'prepplaats'=>$prepplaats, 'prepbedrijfsnaam'=>$prepbedrijfsnaam);
}
$first20 = array_splice($array,0,20);
shuffle($first20);
array_splice($array,0,0,$first20);
$number = count($array);
$cnt = $number; // if you wanted to get them all you could set it to $cnt = array_count($array);
for ($i=0; $i<=$cnt; $i++) {
echo $array[$i]['prepid'];
echo "<Br />";
}
What i would do is put it all into an array like you said and then echo it out. 我要做的就是像您所说的那样将其全部放入一个数组中,然后将其回显。
$array = array();
while($stmt->fetch()) {
$array[] = array('prepid'=>$prepid, 'prephoofdrubriek'=>$prephoofdrubriek, 'prepplaats'=>$prepplaats, 'prepbedrijfsnaam'=>$prepbedrijfsnaam);
}
I have only put a few in the array to demonstrate how to do it. 我只在数组中展示了一些方法。
Then to get the data out I would use a for loop. 然后要获取数据,我将使用for循环。
$cnt = 20; // if you wanted to get them all you could set it to $cnt = array_count($array);
foreach ($i = 0; $i <= $cnt; $i++) {
echo $array[$i]['prepid'];
}
You can do like the following 你可以像下面这样
$arr_val=array();
while($stmt->fetch()) {
$arr_val[] = $stmt->fetch() //or your result row;
}
$sliced_arr = array_slice($arr_val, 0, 20);
array_splice($arr_val, 0, 20);
shuffle($sliced_arr);
$arr_new_val = array_merge($sliced_arr,$arr_val);
print_r($arr_new_val);
Save the $stmt->fetch()
into a variable and add a parameter . 将$stmt->fetch()
保存到变量中并添加一个参数 。
$result = $sth->fetch(PDO::FETCH_ASSOC);
For multiple rows: 对于多行:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result = array_push($result, $row);
}
Now you've extracted your data and you can array_splice
it 现在,您已经提取了数据,并且可以将其array_splice
$first20 = array_splice($result ,0,20);
shuffle($first20);
array_splice($result,0,0,$first20);
Why not simply use fetchAll()
? 为什么不简单地使用fetchAll()
呢?
$arr = array_slice($stmt->fetchAll(), 0, 20);
shuffle($arr);
foreach ($arr as $a)
{
echo $a['prepaid'];
}
If that takes up to much memory (as fetchAll()
returns all rows) you can use fetch()
but limit to 20 (no need to slice). 如果那占用大量内存(因为fetchAll()
返回所有行),则可以使用fetch()
但限制为20(无需切片)。
$i = 0;
$arr = array();
while (($row = $stmt->fetch()) && $i++ < 20)
{
$arr[] = $row;
}
shuffle($arr);
foreach ($arr as $a)
{
echo $a['prepaid'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.