Basic use of PDO

1, database design

create table book(

id int(4) not null primary key auto_increment,

name varchar(255) not null,

author varchar(255) not null)

CHARSET=utf8;

微信图片_20180305101021.png

2, insert test data

##insert into book values

(1,'php basic tutorial','smile1'),

(2,'php intermediate tutorial','smile2'),

(3,'php advanced tutorial','smile3');

Database display:

微信图片_20180305102309.png

##3. Set the database connection variables


       

4, PDO connects to the database

getMessage().'
'; }

5, executes the sql statement and prints

$sql='select *from book'; $result=$pdo->query($sql); $row=$result->fetchAll(); echo "
"; print_r($row); echo "
";

Print result display:

微信图片_20180305102005.png

In actual operation, sometimes you only need to get the index array. In this case, you only need to change the fetchAll() function. The parameters can be
## Code:

fetchAll(PDO::FETCH_ASSOC); //获取索引数组
The print result is as follows:

微信图片_20180305125838.pngIf you want to get the value of the second column of the database, the code is as follows:

fetchAll(PDO::FETCH_COLUMN,1);//获取第二列的所有值
Print As follows:

Continuing Learning
||
getMessage().'
'; } $sql='select *from book'; $result=$pdo->query($sql); $row=$result->fetchAll(); echo "
"; print_r($row); echo "
";
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!