0

I have a mysql table which I want to have it as a PHP array. Suppose we have a field call id and another field called name. As there may be not one result in the table I want something like $result[0]['id'] to point the first result's id. I thought of this:

$result = mysql_query("SELECT * FROM db_name
                                 WHERE dependence = 0");
            $rows = array();
            while($row = mysql_fetch_assoc($result)){
                $rows = $row;
            }
            echo $rows[0]['name'];

But it doesn't work!!! Would you please help me?

0

3 Answers 3

2
$result = mysql_query("SELECT * FROM db_name
                             WHERE dependence = 0");
$rows = array();
while($row = mysql_fetch_assoc($result)){
   $rows[] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

1

try this

     $i=0;
     while($row = mysql_fetch_assoc($result)){
         $rows[$i] = $row['id'];
         $rows[$i] = $row['name']; 
      $i++;  
     }

1 Comment

Solution is fine, however, you don't even need $i and since $row is a named array, you can just add the row to the new array..
0

You forgot to give column name

 while($row = mysql_fetch_array($result)){
    echo $row['colname'];//fill inside which column you need $row['colname']
 }

Comments

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.