[英]Get mysql results according to an array
I want to get mysql results according to an array. 我想根据数组获取mysql结果。
I have this code but it brings one result and repeat it and i want all results 我有这段代码,但它带来一个结果并重复一次,我想要所有结果
$connectdb = mysql_connect('localhost','root','') or die('nonno');
$selectdb = mysql_select_db('test',$connectdb) or die('fofofo');
$se_right = mysql_query("select * from ads ") or die(mysql_error());
$row = mysql_fetch_object($se_right);
$array = array(
"id" => $row->id,
"name" => $row->adsurl
);
$se_ridght = mysql_query("select * from ads ") or die(mysql_error());
while($roww = mysql_fetch_object($se_ridght))
{
foreach($array as $key =>$results)
{
echo $key.':'.$results.'<br />';
}
}
Try this. 尝试这个。
$connectdb=mysql_connect('localhost','root','') or die('nonno');
$selectdb= mysql_select_db('test',$connectdb) or die('fofofo');
$se_right = mysql_query("select * from ads ") or die(mysql_error());
$row = mysql_fetch_object($se_right);
$array = array(
"id" => $row->id,
"name" =>$row->adsurl
);
$index = 0;
while($roww = mysql_fetch_assoc($se_ridght))
{
$yourArray[$index] = $roww;
$index++;
}
}
print_r($yourArray); 的print_r($ yourArray);
Your issue seems to be because you are setting $array
only once and outside of the while loop. 您的问题似乎是因为您只在一次$array
while循环之外设置$array
一次$array
。 The following code builds the $array in the while loop. 以下代码在while循环中构建$ array。 After the loop is finished, the $array
variable is available for use later in your project. 循环完成后, $array
变量可在项目中稍后使用。
$connectdb=mysql_connect('localhost','root','') or die('nonno');
$selectdb= mysql_select_db('test',$connectdb) or die('fofofo');
$array = array();
$se_ridght = mysql_query("select * from ads ") or die(mysql_error());
while($roww = mysql_fetch_object($se_ridght)){
$array[] = array("id" => $roww->id, "name" => $roww->adsurl);
echo $roww->id . ':' . $roww->adsurl . '<br />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.