Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In PHP, you can get the field information from the result, like so (stolen from a project I wrote long ago):</p> <pre><code>/* Similar to mysql_fetch_assoc(), this function returns an associative array given a mysql resource, but prepends the table name (or table alias, if used in the query) to the column name, effectively namespacing the column names and allowing SELECTS for column names that would otherwise have collided when building a row's associative array. */ function mysql_fetch_assoc_with_table_names($resource) { // get a numerically indexed row, which includes all fields, even if their names collide $row = mysql_fetch_row($resource); if( ! $row) return $row; $result = array(); $size = count($row); for($i = 0; $i &lt; $size; $i++) { // now fetch the field information $info = mysql_fetch_field($resource, $i); $table = $info-&gt;table; $name = $info-&gt;name; // and make an associative array, where the key is $table.$name $result["$table.$name"] = $row[$i]; // e.g. $result["user.name"] = "Joe Schmoe"; } return $result; } </code></pre> <p>Then you can use it like this:</p> <pre><code>$resource = mysql_query("SELECT * FROM user JOIN question USING (user_id)"); while($row = mysql_fetch_assoc_with_table_names($resource)) { echo $row['question.title'] . ' Asked by ' . $row['user.name'] . "\n"; } </code></pre> <p>So to answer your question directly, the table name data is always sent by MySQL -- It's up to the client to tell you where each column came from. If you really want MySQL to return each column name unambiguously, you will need to modify your queries to do the aliasing explicitly, like @Shabbyrobe suggested.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload