————————
mysql_field_table is a function to get the data table name.
Under normal circumstances, sql select only queries 1 table, and it can be executed.
Excuse me: when sql select queries multiple tables in union , how to get multiple table names?
——————
Executable code:
//sql select only queries 1 piece of data
<code>$sql = " SELECT id,title FROM `TABLE1` WHERE id IN($id) ORDER BY id DESC LIMIT 0,10 "; $result = mysql_query($sql); $table_name = mysql_field_table($result,0); echo "<br />表名:$table_name";</code>
——————
Question code:
<code>//合并2个表,按时间倒序取出前10条记录 $sql = " SELECT id,title FROM `TABLE1` WHERE id IN($id) UNION SELECT id,title FROM `TABLE2` WHERE id IN($id) UNION ORDER BY id DESC LIMIT 0,10 "; $result = mysql_query($sql); if( $result ){ if( mysql_num_rows($result) ){ $a = array(); while($arr = mysql_fetch_array($result)){ $table_name = mysql_field_table($result,0); echo "<br />表名:$table_name"; //***就是在这里 //***请问各位,怎么获取多个表名 } } }</code>
——————
Salute,
Thank you!
————————
mysql_field_table is a function to get the data table name.
Under normal circumstances, sql select only queries 1 table, and it can be executed.
Excuse me: when sql select queries multiple tables in union , how to get multiple table names?
——————
Executable code:
//sql select only queries 1 piece of data
<code>$sql = " SELECT id,title FROM `TABLE1` WHERE id IN($id) ORDER BY id DESC LIMIT 0,10 "; $result = mysql_query($sql); $table_name = mysql_field_table($result,0); echo "<br />表名:$table_name";</code>
——————
Question code:
<code>//合并2个表,按时间倒序取出前10条记录 $sql = " SELECT id,title FROM `TABLE1` WHERE id IN($id) UNION SELECT id,title FROM `TABLE2` WHERE id IN($id) UNION ORDER BY id DESC LIMIT 0,10 "; $result = mysql_query($sql); if( $result ){ if( mysql_num_rows($result) ){ $a = array(); while($arr = mysql_fetch_array($result)){ $table_name = mysql_field_table($result,0); echo "<br />表名:$table_name"; //***就是在这里 //***请问各位,怎么获取多个表名 } } }</code>
——————
Salute,
Thank you!
The result set after union is regarded as the same database table, so no matter how you get mysql_field_table here, you can only get one table name. The correct approach is that when you construct a SQL statement, attach the name of the database table it comes from to each record, so that you can know which database table this data comes from from the result set.
<code>$sql = " SELECT id,title,'TABLE1' AS ComesFrom FROM `TABLE1` WHERE id IN($id) UNION SELECT id,title,'TABLE2' FROM `TABLE2` WHERE id IN($id) UNION ORDER BY id DESC LIMIT 0,10 ";</code>