How to assign 4 column from database to 4 variable ?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
smashhell
Forum Newbie
Posts: 14
Joined: Fri May 22, 2009 2:54 am

How to assign 4 column from database to 4 variable ?

Post 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 !
Last edited by Benjamin on Tue May 26, 2009 11:23 am, edited 1 time in total.
Reason: Added [code=php] tags.
sumeshkcentral
Forum Newbie
Posts: 1
Joined: Tue May 26, 2009 2:30 am

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

Post 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++;
}
Last edited by Benjamin on Tue May 26, 2009 11:23 am, edited 1 time in total.
Reason: Added [code=php] tags.
smashhell
Forum Newbie
Posts: 14
Joined: Fri May 22, 2009 2:54 am

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

Post 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;
}
Last edited by Benjamin on Tue May 26, 2009 11:24 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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.
smashhell
Forum Newbie
Posts: 14
Joined: Fri May 22, 2009 2:54 am

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

Post 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. :(
Last edited by Benjamin on Tue May 26, 2009 11:24 am, edited 1 time in total.
Reason: Added [code=php] tags.
Post Reply