How does pdo obtain all fields?

Source: Internet
Author: User
How does pdo obtain all fields ?? At last, this post was edited by rainxies from 2013-03-11:08:21. I only found the mysql method. with pdo, how should I write the following code? Thank you $ result & nbsp ;=& nbsp; mysql_query ("SELECT & nbsp; * & n pdo how do I obtain all fields ??
At last, this post was edited by rainxies at 13:08:21. I only found the mysql method. if I use pdo, how should I write the following code? Thank you.

$result = mysql_query("SELECT * FROM table");
$fields = mysql_num_fields($result);
for ($i=0; $i < $fields; $i++) {
   $names[] = mysql_field_name($result, $i);
}
print_r($names);


Mysql_num_fields I have found a columnCount () function under PDOStatement.
Mysql_field_name: I did not find the corresponding name .........

Is there another way ??
------ Solution --------------------
This post was last edited by xuzuning at 13:52:28
Try {
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', 'root ','');
$ Stmt = $ dbh-> query ("select * from tbl_name ");
For ($ I = 0; $ I <$ stmt-> columnCount (); $ I ++ ){
Echo $ stmt-> getColumnMeta ($ I) ['name'], PHP_EOL; // applicable to php5.4. the previous version should be split into two sentences.
}
} Catch (PDOException $ e ){
Die ("Error! : ". $ E-> getMessage ());
}

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.