Is there a way to retrieve the column names of a query that returns no data?
I'm using (somewhat) complicated queries such as:
SELECT
i.*,
ic1.permalink as category_permalink,
ic1.title as category_title,
ic1.sid as category_sid,
ic2.permalink as hook_category_permalink,
ic2.title as hook_category_title,
ic2.sid as hook_category_sid
FROM item i
LEFT JOIN item_to_item_category itic ON i.sid = itic.item_sid
LEFT JOIN item_category ic1 ON ic1.sid = itic.item_category_sid
LEFT JOIN item_category ic2 ON ic1.hook = ic2.sid
WHERE i.uid = ''
LIMIT 0,1
The result of this query would be empty because of WHERE i.uid = ""
. Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE
and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
Anyone?
For PDO, try PDOStatement->getColumnMeta()
. For mysqli, there's mysqli_result->fetch_fields()
, mysqli_result->fetch_field()
and mysqli_result->fetch_field_direct()
. For mysql, replace it with PDO or mysqli.
Here is a simple class based on mysqli . This method will work on empty tables as well. Of course, add your own error checking as needed.
Success will return a numerically indexed array of column names from $table.
Failure or non-string input will return . 。
class mysqlz extends mysqli {
function fetch_table_columns($table) {
if(!is_string($table)) {
return false;
} elseif($obj = $this->query("SHOW COLUMNS FROM " . $table)) {
while($fields = $obj->fetch_object()) {
$columns[] = $fields->Field;
}
return $columns;
} else {
return false;
}
}
}
To use, first instantiate your database as usual:
$mysql = new mysqlz($host, $user, $pass, $database);
Then, call the method to retrieve the column names:
$columns = $mysql->fetch_table_columns("table_name_here");
Depending on your table, the resulting array should similar to this: 类似:
array(3) { [0]=> string(2) "id" [1]=> string(10) "first_name" [2]=> string(9) "last_name" }
If method returns false (var_dump returns bool(false)
) you can check the current error with $mysqlz->error
. For more information on error checking, take a look at the PHP mysqli manual at http://www.php.net/manual/en/book.mysqli.php .
Assuming you are calling the query from PHP. You can call mysqli_fetch_fields even with an emtpy query.
For AdoDB you can use:
$db->Execute("SELECT .......");
$colInfo = $res->FieldTypesArray();
The output is similar to method MetaColumns($table)
, but you can use it on every result set not only for tables (including joins). It works for empty results or empty tables too.
Thanks to outis , I found this function after searching "fetch_field()" in adoDBs mysql-driver :D
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.