[英]how do i use loop in select query
How do I use loop in select query? 如何在选择查询中使用循环? Query only fetch one Row, at a time, from Select Query.
该查询一次只能从“选择查询”中提取一行。 I want to fetch multiple rows In the Select Query but through a loop
我想在选择查询中通过循环获取多行
Select ID 选择ID
$id=$_REQUEST['id'];
function get_id($id){
$result1=mysql_query("select * from products where id='$id'")
or die("Id Problem"."<br/><br/>".mysql_error());
$results1= array();
$k=0; // add the new line
while($row1=mysql_fetch_assoc($result1)){
$results1[] = $row1['id'];
$k++;
}
return $results1;
}
Pid Array 点阵
$pid1=get_id($id);
<?php
$max1=count($pid1);
for($n=0; $n<$max1; $n++)
{?>
<input type="hidden" name="pid[]" value="<?php echo $pid1[$n]?>" />
<?php }?>
Pid Session 竞价时段
$_SESSION['pid']=$_POST['pid'];
I want fetch
Multiple Rows In Select Query But through Loop 我想在选择查询中通过循环
fetch
多行
<?php
$pid = join(',',$_SESSION['pid']);
$result=mysql_query("SELECT id AS wid FROM mywishlist
where pid='$pid'")
or die("Id Problem"."<br/><br/>".mysql_error());
$results= array();
$i=0; // add the new line
while($row=mysql_fetch_array($result)){
$results[$i] = $row['wid'];
$i++;
}
echo $results;
$max=count($results);
for($j=0; $j<$max; $j++)
{
?>
<input type="text" name="wid[]" value="<?php echo $results[$j]; ?>" />
<?php }?>
You're doing some things the hard way, tracking and controlling things that would run fine on autopilot. 您正在用困难的方式做一些事情,跟踪和控制在自动驾驶仪上可以正常运行的事情。 For example - for loops require you to know the length while foreach loops just run 'til they're done.
例如,for循环要求您知道长度,而foreach循环只是运行直到完成。
Letting PHP take care of the array index by using [] instead of assigning the index will also save you some effort. 让PHP通过使用[]而不是分配索引来处理数组索引也可以节省您的精力。 And since each of your loops really only does one thing, they don't really need braces.
而且由于每个循环实际上只做一件事,因此它们实际上并不需要大括号。
$result=mysql_query('SELECT id AS wid FROM mywishlist where pid="'.$pid.'"') or die();
$results= array();
while($row=mysql_fetch_array($result))
$results[]=$row['wid'];
foreach($results as $wid)
echo '<input type="text" name="wid[]" value="'.$wid.'" />';
Of course, you could eliminate one of these loops because they both essentially handle the same data... 当然,您可以消除这些循环之一,因为它们本质上都处理相同的数据...
$result=mysql_query('SELECT id AS wid FROM mywishlist where pid="'.$pid.'"') or die();
while($row=mysql_fetch_array($result))
echo '<input type="text" name="wid[]" value="'.$row['wid'].'" />';
Furthermore, reidentifying the key from id to wid doesn't make much sense. 此外,从id到wid的密钥重新标识没有多大意义。 Since there is only 1 field in the result, why bother identifying it - we can just fetch the row.
由于结果中只有1个字段,为什么还要麻烦识别它-我们可以直接获取该行。
$result=mysql_query('SELECT id FROM mywishlist where pid="'.$pid.'"') or die();
while($row=mysql_fetch_row($result))
echo '<input type="text" name="wid[]" value="'.$row[0].'" />';
Ooh, let's do it in 2 lines 哦,让我们分两行来做
$result=mysql_query('SELECT id FROM mywishlist where pid="'.$pid.'"') or die();
while($row=mysql_fetch_row($result)) echo '<input type="text" name="wid[]" value="'.$row[0].'" />';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.