php mysql PDO 查询操作的实例详解

<"INSERT INTO `user` SET `login`=:login AND `password`=:password";  
$sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)"; $stmt = $dbh->prepare($sql); $stmt->execute(array(':login'=>'kevin2',':password'=>''));  
echo $dbh->lastinsertid();  
/*修改*/ 
$sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId";  
$stmt = $dbh->prepare($sql);  
$stmt->execute(array(':userId'=>'7', ':password'=>'4607e782c4d86fd5364d7e4508bb10d9'));  
echo $stmt->rowCount();  
/*删除*/ 
$sql = "DELETE FROM `user` WHERE `login` LIKE 'kevin_'"; //kevin%  
$stmt = $dbh->prepare($sql);  
$stmt->execute();  
echo $stmt->rowCount();  
/*查询*/ 
$login = 'kevin%';  
$sql = "SELECT * FROM `user` WHERE `login` LIKE :login";  
$stmt = $dbh->prepare($sql);  
$stmt->execute(array(':login'=>$login));  
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){    
 print_r($row);  
}  
print_r( $stmt->fetchAll(PDO::FETCH_ASSOC));  
"htmlcode">
<"htmlcode">
<"SET CHARACTER SET utf8"); 
$dbh=null; //断开连接 
}catch(PDOException$e){ 
print"Error!:".$e->getMessage()."<br/>"; 
die(); 
} 
"htmlcode">
<"insertintostaff(id,first,last)values(23,'Joe','Bloggs')"); 
$dbh->exec("insertintosalarychange(id,amount,changedate) 
values(23,50000,NOW())"); 
$dbh->commit();//提交事务 
 
}catch(Exception$e){ 
$dbh->rollBack();//错误回滚 
echo"Failed:".$e->getMessage(); 
} 
"htmlcode">
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); //不显示错误
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//显示警告错误,并继续执行
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//产生致命错误,PDOException
<"/t";   
 }  
}  // if there is a problem we can handle it here  
catch (PDOException $e) {   
 echo 'PDO Exception Caught. ';   
 echo 'Error with the database: <br />';   
 echo 'SQL Query: ', $sql;   
 echo 'Error: ' . $e->getMessage();  
}  
"htmlcode">
<"/t";   
 print $row['CountryCode'] . "/t";   
 print $row['Population'] . "/n";  
}  
"htmlcode">
<"htmlcode">
<"Insert INTO `test`.`table` (`name` ,`age`)VALUES ('mick', 22);");  
 $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('lily', 29);");  
 $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('susan', 21);");  
 $dbh->commit();  
 } catch (Exception $e) {  
 $dbh->rollBack();  
 echo "Failed: " . $e->getMessage();  
 }  
"select ...")方法时,获取记录的条数。也可以用于预处理中。$stmt->rowcount();
PDOStatement::columncount()适用于当用query("select ...")方法时,获取记录的列数。

注解:

1、选fetch还是fetchall?

小记录集时,用fetchall效率高,减少从数据库检索次数,但对于大结果集,用fetchall则给系统带来很大负担。数据库要向WEB前端传输量太大反而效率低。

2、fetch()或fetchall()有几个参数:

mixed pdostatement::fetch([int fetch_style [,int cursor_orientation [,int cursor_offset]]])
array pdostatement::fetchAll(int fetch_style)

fetch_style参数:

■$row=$rs->fetchAll(PDO::FETCH_BOTH); FETCH_BOTH是默认的,可省,返回关联和索引。
■$row=$rs->fetchAll(PDO::FETCH_ASSOC); FETCH_ASSOC参数决定返回的只有关联数组。
■$row=$rs->fetchAll(PDO::FETCH_NUM); 返回索引数组
■$row=$rs->fetchAll(PDO::FETCH_OBJ); 如果fetch()则返回对象,如果是fetchall(),返回由对象组成的二维数组

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:
php,mysql,PDO,php,mysql,PDO的查询,php,mysql

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“php mysql PDO 查询操作的实例详解”

暂无“php mysql PDO 查询操作的实例详解”评论...