Home  >  Article  >  Backend Development  >  PDO queries one row of data at a time

PDO queries one row of data at a time

不言
不言Original
2018-04-16 15:44:082871browse

The main content of this article is about PDO querying one row of data at a time, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

<?php
  require &#39;pdo_edu_config.php&#39;;
try{
  $pdo=new PDO($dsn,$userName,$passWord);
  $sql="SELECT id,name,course,grade FROM student WHERE grade>70";
  $result=$pdo->query($sql);//返回一个PDOstatement结果集对象
  if($result && $result->rowCount()){//判断结果集对象是否存在,并且结果集数量是否大于0,也就是说是否存在数据
    //rowCount()是结果集中的一个方法,可以返回当前结果集中的记录条数
    $result->setFetchMode(PDO::FETCH_ASSOC);//设置结果集的读取方式,这里用的是关联的方式进行读取
    $row=$result->fetch();//获取记录
    print_r($row);
  }
}catch(PDOException $e){
  die($e->getMessage());
}


You can use $result->rowCount>0 to determine whether the data has been queried, which can be used for query judgment when the user logs in.

Related recommendations:

PHP database is based on PDO operation Class(mysql)


The above is the detailed content of PDO queries one row of data at a time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn