简体   繁体   中英

get table name from count query

How do you get table name from count query, I have multiple queries and I need to get table name from the query results, here is my query

$myquery = "select count(tb_id) as num_rows from table1; select count(tb1_id) from table2...";
if (myqli_multi_query($connection, $myquery){
..
$row = mysqli_fetch_assoc($result);
$num_rows = $row["num_rows"];
..
}

The query is working fine as I can get the number of rows, but I am unable to link the table name to the number of rows (result)

I have tried this, which executes without error, but unable to get the table name still

select count(tb_id) as num_rows, 'table1' as TableName from table1

You could put an identifier into your query you can refer to later.

$myquery = "select 'table1' as tablename, count(tb_id) as num_rows from table1; 
select 'table2' as tablename, count(tb1_id) as num_rows from table2;"
// and so on

Just add it in to the select or the column name.

Column name:

select count(tb_id) as table1_count from table1; select count(tb1_id) as table2_count from table2.

OR sep column:

(select count(tb_id) as num_rows, 'table1' as tablename from table1)
UNION
(select count(tb1_id), 'table2' from table2...)
UNION.... etc

Edited to add: you are returning sep resultsets; consider UNION above for a single resultset.

您可以尝试mysqli_fetch_field

mysqli_fetch_field($result)->table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM