根据数组获取mysql结果

时间:2013-01-12 06:15:00

标签: php mysql

我想根据数组得到mysql结果。

我有这个代码,但它带来一个结果并重复它,我想要所有结果

$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 />';
    }
}

2 个答案:

答案 0 :(得分:3)

试试这个。

$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);

答案 1 :(得分:1)

您的问题似乎是因为您只在{while}循环之外设置$array一次。以下代码在while循环中构建$数组。循环结束后,$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 />';
}