Home > php教程 > php手册 > body text

PHP PDO->exec() 执行无返回结果集的 SQL 语句

WBOY
Release: 2016-06-13 08:50:42
Original
1273 people have browsed it

PHP PDO->exec() 执行无返回结果集的 SQL 语句

<?php
/*
DO->exec() 方法主要是针对没有结果集合返回的操作,
比如 INSERT、UPDATE、DELETE 等操作,它返回的结
果是当前操作影响的列数
 
语法:PDO->exec( string statement )
*/
    //构造PDO连接
    header("Content-type:text/html;charset=utf-8");
    $dbh = "mysql:host=localhost;dbname=test";
    $db = new PDO($dbh, &#39;root&#39;, &#39;123456&#39;);
    $db->query("set character set &#39;utf8&#39;");
    //写入数据
    $username="liming";
    $password = md5("123456");   
    $regdate = time();//返回的是一个整形,所以下面语句中可以不带引号
    $sql_exec = "INSERT INTO userlist (username,password,regdate)VALUES(&#39;$username&#39;,&#39;$password&#39;,
    $regdate)";
    $count = $db->exec($sql_exec);
    echo &#39;写入 &#39;.$count.&#39; 条数据记录!&#39;;
 
    echo "<hr/>";
    $sql_select = "SELECT * FROM userlist";
    $sth = $db->query($sql_select);//$sth 是结果集对象
    //$sth->setFetchMode(PDO::FETCH_ASSOC);// 如果不在setFetchMode()中指定返回的结果类型,也可以单独使用fetch()方法设定
    while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    print_r($row);
    echo "用户名:".$row[&#39;username&#39;]."  ";
    echo "密码:".$row[&#39;password&#39;]."  ";
    echo "注册时间:".date("Y-m-d",$row[&#39;regdate&#39;])." &nbsp";
    }
 
?>
Copy after login

 

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template