2

我想根据数组获取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 />';
    }
}
4

2 回答 2

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

于 2013-01-12T06:19:25.613 回答
1

您的问题似乎是因为您只设置$array了一次并且在 while 循环之外。以下代码在 while 循环中构建 $array。循环完成后,该$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 />';
}
于 2013-01-12T06:20:26.927 回答