Page 1 of 1

How to assign 4 column from database to 4 variable ?

Posted: Tue May 26, 2009 12:50 am
by smashhell
Sorry about this, but another big problem for me.
I can not figure out how to get 4 different columns from database and assigning them to 4 different variable. :(
Here is what I got so far:

Code: Select all

 
$query = mysql_query("SELECT * FROM table ORDER BY id DESC", $connection);
while ($row = mysql_fetch_array($query)) {
   $array[] = $row['id'];
   $id = $array;
   $array[] = $row['name'];
   $name = $array;
   $array[] = $row['grade'];
   $grade = $array;
   $array[] = $row['class'];
   $class = $array;
}
This of course, doesn't work at all. :banghead:
All four variable $id,$name,$grade,$class have the same array in them.
So how do I get each column into their own variable ?

Thank you in advance !

Re: How to assign 4 column from database to 4 variable ?

Posted: Tue May 26, 2009 2:41 am
by sumeshkcentral
Please try this..

Code: Select all

 
$query = mysql_query("SELECT * FROM table ORDER BY id DESC", $connection);
$i=0;
$array=array();
while ($row = mysql_fetch_array($query)) {
$array[$i]['id'] = $row['id'];
$array[$i]['name'] = $row['name'];
$array[$i]['grade'] = $row['grade'];
$array[$i]['class'] = $row['class'];
$i++;
}

Re: How to assign 4 column from database to 4 variable ?

Posted: Tue May 26, 2009 5:01 am
by smashhell
First of all, thank you for your reply sumeshkcentral.
But I actually could achieve the same result easier.
Though, they are still not in different variable.
Is it impossible to assign one column from database to one array ? :(
Thank you for your answer anyway.

Code: Select all

 
//Below gives the exact same result as your code
$query = mysql_query("SELECT * FROM table ORDER BY id DESC", $connection);
while ($row = mysql_fetch_array($query)) {
   $array[] = $row;
}

Re: How to assign 4 column from database to 4 variable ?

Posted: Tue May 26, 2009 5:32 am
by onion2k
smashhell wrote:Is it impossible to assign one column from database to one array ? :(
One of PHP's greatest failings is that there's so much written for you that people expect everything to be written for you. This is an example of something that you will have to write yourself. Rather than whining that you don't know how to do it, and saying it might be impossible, how about thinking about the problem for a few minutes and working out how to solve it?

This forum is here to help. It is not here to provide software development for free. If you have a question about how to solve the problem then feel free to ask. If you just want someone to write the code for you, go somewhere else.

Re: How to assign 4 column from database to 4 variable ?

Posted: Tue May 26, 2009 6:36 am
by smashhell
onion2k wrote:
smashhell wrote:Is it impossible to assign one column from database to one array ? :(
One of PHP's greatest failings is that there's so much written for you that people expect everything to be written for you. This is an example of something that you will have to write yourself. Rather than whining that you don't know how to do it, and saying it might be impossible, how about thinking about the problem for a few minutes and working out how to solve it?

This forum is here to help. It is not here to provide software development for free. If you have a question about how to solve the problem then feel free to ask. If you just want someone to write the code for you, go somewhere else.
First of all, I am not the kind of person that ask someone for their codes. In fact, I don't even copy and paste my own codes.
Second, yeah of course I spent a ton of time trying to solve the problem.
I am a month new into PHP and I only came here to ask because I can't solve it.

Anyone, here is mine "tenth" attempt of trying to solve my problem.
Again, I just want someone to tell me whats wrong with my code.
Thank you.

Code: Select all

 
$query = "SELECT * FROM table ORDER BY id DESC";
$run = mysql_query($query,$connection);
  $num = 0;
  while($row = mysql_fetch_array($run)) {
     $id[] = $row[$num]['id'];
     $num++;
        }
     }
 
*That failed as well. It return the correct number of slots but no info inside, not even the id number. I seriously don't know how to solve this. :(