2

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 />';
    }
}
1
  • what do you mean by accoeding to an array? Commented Jan 12, 2013 at 6:38

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

it returns the first result of mysql table iwant it to returns it all
I have made some edits to the post...I hope you will get the result now @Flenston F...
1

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

2 Comments

returns Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\xampp\htdocs\~34~ads2.php on line 14
Try again with $array[] = array("id" => $roww->id, "name" => $roww->adsurl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.