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;
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:
##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 result display:"; print_r($row); echo "";
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:
If 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: