pdo的基本使用

查询数据用query
$dsn = "mysql:host=127.0.0.1;dbname=php7";
$pdo = new PDO($dsn, 'root', 'root');
$pdo->query('set names utf8');
$result = $pdo->query('select * from day1');
$result->setFetchMode(PDO::FETCH_ASSOC);//为语句设置默认的获取模式。  
$result_arr=$result->fetchAll();
print_r($result_arr);


//建立一个表单页面

<html>
<table>
<form action="form.php" method='post'>
    <input type='text' name='name'/>
    <input type='password' name='pwd'/>
    <input type='submit' value='登陆'>
</form>
</table>

</html>



//跳转到另外一个表单(form.php)页面,进行pdo各项的使用,并且用pdo进行各种处理

<?php
$dsn = "mysql:host=127.0.0.1;dbname=php7";//连接数据库
$db = new PDO($dsn,'root','root');//数据库账号密码
$name=$_POST['name'];//接值
//$name="zhangsan' or 'a' ='a";
$pwd=$_POST['pwd'];
$sql="select * from pdo1 where name='$name' and pwd ='$pwd'";
$sql="select * from pdo1 where name =? and pwd =?";
$stmt = $db->prepare($sql);
$exeres = $stmt->execute(array($name, $pwd));
if ($exeres) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
}



//引入sql

$dsn = "mysql:host=127.0.0.1;dbname=php7";
$db = new PDO($dsn,'root','root');
$name="zhangsan' or 'a' ='a";
$pwd=$_POST['pwd'];
$sql="select * from pdo1 where name='$name' and pwd ='$pwd'";





你可能感兴趣的:(pdo的基本使用)