PHP的PDO操作实例

时间:2023-03-09 07:28:34
PHP的PDO操作实例

try{
      
       $dbms='mysql';          //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数
       $host='127.0.0.1';      //数据库主机名
       $dbName='books';      //使用的数据库
       $user='root';           //数据库连接用户名
       $pass='';              //对应的密码
       $dsn="$dbms:host=$host;dbname=$dbName";
       $pdo = new PDO($dsn, $user, $pass);  //初始化一个PDO对象,就是创建了数据库连接对象$pdo
       
       */
       //echo $_POST['bookname'];
       //插入出数据库语句,图片数据前要加上0x,用于表示16进制数
       //$query = "insert into book(name,createtime,price,author,publish) value('$bookname','$booktime','$bookprice','$bookauthor','$bookpublish')";
       //$query = "insert into images(pic) values('".$name."','".$type."',0x".$imgdata.")")  
       //$query = "insert into book(name,createtime,price,author,publish,pic) values(':name',':createtime',':price',':author',':publish',0x".$imgdata.")";
       //$query = "insert into book(name,createtime,price,author,publish,pic) values(?,?,?,?,?,0x".$imgdata.")";
       //$result=$pdo->prepare($query);   //准备查询语句
       //$result=$pdo->prepare("insert into book(name,createtime,price,author,publish,pic) values(?,?,?,?,?,0x".$imgdata.")");   //准备查询语句
       //$result=$pdo->prepare("insert into book(name,createtime,price,author,publish,pic) values(?,?,?,?,?,0x.?)"); 
       //$result=$pdo->prepare("insert into book(name,createtime,price,author,publish) values(?,?,?,?,?)");
       //echo $_POST['imagedata'];
       $result=$pdo->prepare("insert into book(name,createtime,price,author,publish,pic) values(?,?,?,?,?,?)");
       //echo $_POST['bookname'];
       /*
       
       $result->bindValue(1,$_POST['bookname']);
       $result->bindValue(2,$_POST['booktime']);
       $result->bindValue(3,$_POST['bookprice']);
       $result->bindValue(4,$_POST['bookauthor']);
       $result->bindValue(5,$_POST['bookpublish']);
       $result->bindValue(6,$_POST['imagedata']);
       */
      // echo "<h1>".time()."</h1>"
      // echo $_POST['imagedata'];
       
       
       
       /*
       $result->bindParam(':name', $_POST['bookname']);
       $result->bindParam(':createtime',$_POST['booktime']);
       $result->bindParam(':price', $_POST['bookprice']);
       $result->bindParam(':author',$_POST['bookauthor']);
       $result->bindParam(':publish',$_POST['bookpublish']);
       */  
  /*      
        
        if($result->execute() > 0)
        {
         echo "<br/> insert pic success\n";
         echo "<center>insert success!<br><br><a href='display.php'>!!!!!!!!!!!!!!qq</a></center>";
        }
        else
        {
         echo "<center>insert failed 26".mysql_error()."</center>";
        }
      }
      catch(PDOException $e)
      {
       die("Error:".$e->getMessage()."<br/>");
      }