phpコース---PDO初心者

4428 ワード

<!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();
    }
    
    
    $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')); */
    
    //$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>