Flenston F
Flenston F

Reputation: 51

Get mysql results according to an array

I want to get mysql results according to an array.

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

Upvotes: 2

Views: 59

Answers (2)

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

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

Upvotes: 3

PassKit
PassKit

Reputation: 12581

Your issue seems to be because you are setting $array only once and outside of the while loop. The following code builds the $array in the while loop. After the loop is finished, the $array variable is available for use later in your project.

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

Upvotes: 1

Related Questions