2

I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.

my current code:

<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");

$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{

$solution=$row['solution'];
}
?>

The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.

1
  • So you want an multidimensional array of each row? Why not just add each row to the array each loop? Commented Jun 28, 2011 at 2:46

2 Answers 2

3

You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.asp as well as my code.

A very simple way (obviously I haven't included mysql_num_rows etc):

$solutions = array()

while($row = mysql_fetch_assoc($result)) {
  $solutions[] = $row['solution'];
}

If you have three in your result solutions will be:

$solutions[0] -> first result $solutions[1] -> second $solutions[2] -> third

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

Comments

0
 <?php
      $result=mysql_query("SELECT * FROM table WHERE var='$var'");
      $solution = array();

      $check_num_rows=mysql_num_rows($result);

      while ($row = mysql_fetch_assoc($result))
      {
           $solution[]=$row['solution'];
      }
 ?>

1 Comment

how will i know which solution is which variable

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.