Home > Backend Development > PHP Tutorial > PHP PDO->exec() executes SQL statements without returning a result set_PHP tutorial

PHP PDO->exec() executes SQL statements without returning a result set_PHP tutorial

WBOY
Release: 2016-07-12 09:04:39
Original
1039 people have browsed it

PHP PDO->exec() executes a SQL statement that does not return a result set

<?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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1072631.htmlTechArticlePHP PDO-exec() executes a SQL statement that does not return a result set?php/*DO-exec() method Mainly for operations that do not return a result set, such as INSERT, UPDATE, DELETE and other operations, it returns...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template