Possible Duplicate:
one variable row inside another variable row
I have script something like –
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT * FROM `other_table`";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$row['username']];
How can I set one variable row inside another, since it don’t work. Basically, I need to select username, and then select column with user username from other table, in which is written user points.
I was thinking about adding –
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT `".$row['username']."` FROM `other_table` WHERE `uid` = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[xxxxxxxxxx]; // DONT KNOW HOW TO DEFINE IT, so it takes out found variable (there is only one).
I have question already, but not with full info, and I don’t know how to delete it :(!
Here is my table –
Users
‘id’–‘name’
‘1’–‘Bil’
‘2’–‘Conor’
‘3’–‘Ilian’
Other_table (which holds points for users)
‘id’–‘Bil’–‘Conor’–‘Ilian’
‘1’–‘2’–‘3′–’55’
Don’t ask, why I don’t hold the points in the same table, since if I could, I would do that ;)!
It sounds like you need to redesign the tables to start with, I would suggest a table structure like this:
This way you can add a foreign key constraint between the tables and then do a simple join query like:
Then your php code would look like:
Here are the create table queries for you:
EDIT:
This should allow you to get the value (NOTE: untested as I don’t have a php/mysql instance to mess with currently), but for a more robust solution you really should look into redesigning your table schema.