Do I use PDO to query the database content like this?

Source: Internet
Author: User
Do I use PDO to query the database content like this? Why am I displaying this error?
(! ) Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in D: \ wamp \ www \ cxblog. php on line 4
Is this an array? I just want to extract the values in the database. how should I write the values in the array form?
$ Pdo = new PDO ("mysql: host = localhost; dbname = t1", "root ","");
$ SQL = $ pdo-> query ('select * from blog ')
Foreach ($ SQL as $ row ){
Print_r ($ row );
}
?>


Reply to discussion (solution)

PDOStatement PDO: query (string statement)
Mixed PDOStatement: fetch ([int fetch_style [, int cursor_orientation [, int cursor_offset])
Array PDOStatement: fetchAll ([int fetch_style [, int column_index])


PDOStatement PDO: query (string statement)
Mixed PDOStatement: fetch ([int fetch_style [, int cursor_orientation [, int cursor_offset])
Array PDOStatement: fetchAll ([int fetch_style [, int column_index])


Cannot understand English

PDOStatement PDO: query (string statement)
Mixed PDOStatement: fetch ([int fetch_style [, int cursor_orientation [, int cursor_offset])
Array PDOStatement: fetchAll ([int fetch_style [, int column_index])


I know the problem. I have no ending semicolon before $ SQL. now an array is displayed, but I only want data.

This is the case.

 query('select * from blog') $result = $sth->fetchall(PDO::FETCH_ASSOC); foreach($result as $k=>$v){    print_r($v);}?>

This is the case.

 query('select * from blog') $result = $sth->fetchall(PDO::FETCH_ASSOC); foreach($result as $k=>$v){    print_r($v);}?>


I tried to remind me of this error. why?
(! ) Parse error: syntax error, unexpected '$ result' (T_VARIABLE) in D: \ wamp \ www \ cxblog2.php on line 4

This is the case.

 query('select * from blog') $result = $sth->fetchall(PDO::FETCH_ASSOC); foreach($result as $k=>$v){    print_r($v);}?>


In this way, I only want to get the value in array format.

Of course, it is in the array format!
YOU select * from blog
This * indicates all the fields in the table. it is impossible for php to link them into a value beyond the limit.
Even if there is only one field in the table, php does not know it.
Because all the power of interpretation lies in you, not in php

Of course, it is in the array format!
YOU select * from blog
This * indicates all the fields in the table. it is impossible for php to link them into a value beyond the limit.
Even if there is only one field in the table, php does not know it.
Because all the power of interpretation lies in you, not in php


Only one column in my table is the content column. I can't change it to select content from blog.

As I have already said, php always returns an array for database queries.

$ Pdo = new PDO ("mysql: host = localhost; dbname = t1", "root ",""); $ Something = $ pdo-> query ('select * from blog') $ result = $ Something-> fetchall (PDO: FETCH_ASSOC ); foreach ($ result as $ k =>$ v) {echo $ v ['content']; // is this not the case but the value ?}

As I have already said, php always returns an array for database queries.

$ Pdo = new PDO ("mysql: host = localhost; dbname = t1", "root ",""); $ Something = $ pdo-> query ('select * from blog') $ result = $ Something-> fetchall (PDO: FETCH_ASSOC ); foreach ($ result as $ k =>$ v) {echo $ v ['content']; // is this not the case but the value ?}


Oh, yes. thank you for taking a look.

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.