In mysql-PHP, how does one return the field name?

Source: Internet
Author: User
{Code ...} after using this function, you can return the result set, but there is no field name. Of course, {code...} is used ...} you can use the array key as the field name, but there is no information except the field name. Is there any method like {code ...} can we see all the information about the field?
$pdo->query($SQL); 

When the result set is used, the result set can be returned, but there is no field name,
Of course.

$stmt->fetch( PDO::FETCH_ASSOC )

The array key can be used as the field name,

However, there is no information except the field name,

Is there any method like

$ Result-> fetch_field () of object mysqli ()

Can we see all the information about the field?

Reply content:
$pdo->query($SQL); 

When the result set is used, the result set can be returned, but there is no field name,
Of course.

$stmt->fetch( PDO::FETCH_ASSOC )

The array key can be used as the field name,

However, there is no information except the field name,

Is there any method like

$ Result-> fetch_field () of object mysqli ()

Can we see all the information about the field?

PDO is PDO, and Mysqli is Mysqli. we can't ask PDO to maintain the same structure and design as Mysqli.
You may think that the fetch_field of mysqli is very convenient, but in fact this method is very low, because it usually reads all the field information of the table and caches it, rather than always querying the field.
The most obvious frame is Cakephp. When you add a new field by directly editing the data table, the data will not be saved to this field, the cached data table information must be cleared first.

By reading different PHP frameworks, we can find that there are very few frameworks that require developers to directly call PDO or mysqli objects to execute SQL. They often encapsulate them through interfaces such as DbAdapter. I suggest you think about the benefits of doing so and then optimize your code structure.
This behavior in your problem usually does not need to be encapsulated in the db object, and is generally processed in the Table object, for example

class Table {    protected $_tableName;    public function __construct($tableName);    protected function _readFieldsPrototype();    protected function _getCachedFieldsPrototype();    public function getFieldPrototype($filed);}

For reference only.

sql= "SHOW COLUMNS FROM `your_table`"

The returned result set contains all the fields of your_table and TYPE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.