PDO

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
    
    try{
    $dsn = "mysql:dbname=mydb;host=localhost";//PDO格式
    $pdo = new PDO($dsn,"root","123");
    }
    catch(PDOException $e)                
    {
        echo "连接出错".$e->getMessage();
    }
    $sql="insert into info values('p011','张三','true','n011','1988-1-1')";
    $result = $pdo->exec($sql);//exec 执行语句,返回影响行数 适用于增删改
    if($result) //==1
    {
        echo "chenggong";
        }
    else
    {
        echo "shibai<br />";
        }
    $sql1 = "select * from info";
    $result1 = $pdo->query($sql1);//query返回结果集 适用于查询
    
    //print_r($result->fetch());
    foreach($result1 as $arr)
    {
        print_r($arr);
        echo "<br />";
    }
    
    
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php

    try{
    $dsn = "mysql:dbname=mydb;host=localhost";
    $pdo = new PDO($dsn,"root","123");
    }
    catch(PDOException $e)
    {
        echo "连接出错".$e->getMessage();
    } 
    
    //PDO 预处理  查询
    /*$sql = "select * from Info where code = ?";
    $stmt = $pdo->prepare($sql);
    $result = $stmt->execute(array("p001"));返回结果为1代表true
    $result = $stmt->fetch()用这种方法遍历
     */
    
    $sql = "select * from Info";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();    
     
    while($row = $stmt->fetch())
    {
        print_r($row);
        echo "<br>";
    }
    
    
    //PDO预处理 插入
    
    //第一种 参数用"?"
    /*//写好SQL语句
    $sql = "insert into Info values(?,?,?,?,?)";
    //把SQL语句放在服务器上等待执行
    $stmt = $pdo->prepare($sql);
    
    //将SQL语句中的?绑定上参数
    $stmt->bindParam(1,$code);
    $stmt->bindParam(2,$name);
    $stmt->bindParam(3,$sex);
    $stmt->bindParam(4,$nation);
    $stmt->bindParam(5,$birthday);
    
    //给参数赋值
    $code = "p11";
    $name = "王五";
    $sex = true;
    $nation = "n001";
    $birthday = "1989-2-3";
    
    //执行
    $stmt->execute();
    
    //简便写法 通过数组  执行
    $stmt -> execute(array("p111", '赵六', false, 'n002', '1989-3-4')); 
    //通过$_POST["name"]取的数组可以直接写
     $stmt -> execute($_POST);   */
    
    
    
    //第二种 参数用":"
    //$sql = "insert into Info values(:code, :name, :sex, :nation, :birthday)";
    
    //$stmt = $pdo->prepare($sql);
    /*
    $stmt->bindParam("code", $code, PDO::PARAM_STR);
    $stmt->bindParam("name", $name, PDO::PARAM_STR);
    $stmt->bindParam("sex", $sex, PDO::PARAM_STR);
    $stmt->bindParam("nation", $nation, PDO::PARAM_STR);
    $stmt->bindParam("birthday", $birthday, PDO::PARAM_STR);
    
    $code = "p112";
    $name = "王五";
    $sex = true;
    $nation = "n001";
    $birthday = "1989-2-3";
    
    $stmt->execute();*/
    
    //$stmt->execute(array("code"=>"p020","name"=>"随便","sex"=>true,"nation"=>"n003","birthday"=>"1988-5-6"));
    
?>
</body>
</html>
原文地址:https://www.cnblogs.com/Chenshuai7/p/5201156.html