[TOC]

fetchColumn() 函数坑

1
2
3
4
5
6
7
8
9
#方式1:
$sql_stmt = $link->prepare("SELECT count(*) FROM tea WHERE grade = 3 AND dateline = '2020-03-01'");
$sql_stmt->execute();
$sql_flag = $sql_stmt->fetchColumn(); //返回影响的条数,然后再进行查询语句的执行,以达到不执行空语句;

#方式2:
$sql_stmt = $link->prepare("SELECT * FROM tea WHERE grade = 3 AND dateline = '2020-03-01'");
$sql_stmt->execute();
$sql_flag = $sql_stmt->fetchColumn(); //返回执行SQL影响的那一行的id值(首字段),且后面采用fetchAll()不能读取SQL语句执行过后的数据

执行SQL后进行数据获取

1
2
3
4
5
6
// $stmt = $link->prepare("SELECT * FROM user WHERE grade = ?");
// $stmt -> execute(array($grade));
// $string = null;
// foreach($stmt as $row){
// $name = $row['name'];
// }