PHP / MySQL: How to call a col in a MySQL-Result with more than one table?
If I have a Select Statement like this:
SELECT T1.TE开发者_JAVA技巧XT, T2.FIELD FROM TABLE1 T1, TABLE2 T2 WHERE T1.ID = T2.ID
Then i use it with mysql_query and fetch it with mysql_fetch_object.
How can i access now the fields? $fetched->T1.TEXT doesn't work...
thx for help
UPDATE 2013: This question is quite old, but the correct and simplest way is to give every field an alias in the select statement, so they are unique. Sample:
SELECT T1.TEXT AS MYTEXT, T2.FIELD AS MYFIELD FROM TABLE1 T1, TABLE2 T2 WHERE T1.ID = T2.ID
PHP:
mysql_connect('host','user','pass');
mysql_select_db('mydb');
$resource = mysql_query(/* the query again */);
$firstrow = mysql_fetch_object($resource);
$text = $firstrow->MYTEXT;
$field = $firstrow->MYFIELD;
Have a look at this
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select user_id,fullname from mytable");
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
mysql_free_result($result);
More in php manual about mysql-fetch-object
You can see what fields are in your object by doing this:
var_dump($fetched);
You will probably see the field $fetched->TEXT.
(assuming you are using php)
try this ->
$fetched->{T1.TEXT}
精彩评论