3

When I query a mysql database in PHP, I can not figure out how to specify the table of the field in the result set.

For example, I would like to do the following:

$row = mysql_fetch_assoc($result);

$row["table.FIELD"];

But, can not figure out how. Surely, it is able to happen somehow.

EDIT: I have checked documentation, and have found nothing. I am not sure if I was understood at first... I know how to get the value of a field in a row from a result set. But, I would like to be able to get the value of a field in a row by specifying the name of the table in front of that field.

$row["FIELD"]; vs $row["table.FIELD"];

From the above line, I would like to do the latter.

Thanks,

Steve

3 Answers 3

2

You get it as $row[field_name],
And if you have two fields with same name but from different tables, You must add at least to one of them table.field AS somthing_else

SELECT t1.id,t2.id AS 't2_id' ....
...
...
var_dump($row);

will give $row['id'],$row['t2_id']

While if you don't use the AS you will get only $row['id'] (one value was lost).

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

Comments

1

It's most likely in $row['FIELD'], depending on how you structured your SQL statement.

Try print_r( $row ) to see everything that's in the array.

Comments

-1

The documentation explains how to do what you want, check the examples.

1 Comment

RTFM is rarely a good answer to a question this specific. The OP was asking if there is a native way to include table names in results in order to avoid collisions with names from joined tables. The answer is: there is regrettably no native way - use aliases.

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.