mysql先行編譯處理(mysqli、PDO)

來源:互聯網
上載者:User

標籤:mysql   php   mysqli   pdo   

DML語句先行編譯:

MysqLi:

<?php$mysqli = new mysqli("localhost","root","root","dbname");$mysqli->query("set names utf8");$sql = 'insert into user(id,name,age,email) values (?,?,?,?)';$mysqli_stmt = $mysqli->prepare($sql);$id = 2;$name = 'kung';$age = 28;$email = '[email protected]';$mysqli_stmt->bind_param('isis',$id,$name,$age,$email);$res = $mysqli_stmt->execute();if(!$res){echo 'error'.$mysqli_stmt->error;exit;}else{echo 'ok';}$id = 3;$name = 'xiaoyu';$age = 28;$email = '[email protected]';$mysqli_stmt->bind_param('isis',$id,$name,$age,$email);$res = $mysqli_stmt->execute();if(!$res){echo 'error'.mysqli_stmt->error;exit;}else{echo 'ok';}?>

PDO:

<?php$dns = 'mysql:dbname=dbname;host=127.0.0.1';$user = 'root';$password = 'root';try{$pdo = new PDO($dns,$user,$password);} catch(PDOException $e){echo $e->getMessage();}$pdo->query("set names utf8");$sql = 'inser into user values(:id,:name,:age,:email)';$pdo_stmt = $pdo->prepare($sql);$id = 2;$name = 'kung';$age = 27;$email = '[email protected]';$pdo_stmt->bindParam(':id',$id);$pdo_stmt->bindParam(':name',$name);$pdo_stmt->bindParam(':age',$age);$pdo_stmt->bindParam(':email',$email);$pdo_stmt->execute();?>

DQL語句先行編譯:

mysqli:

<?php$mysqli = new mysqli("localhost","root","root","dbname");$mysqli->query("set names utf8");$sql = " select id,name from user where id > ?";$mysqli_stmt = $mysqli->prepare($sql);$id = 1;$mysqli_stmt->bind_param('i',$id);$mysqli_stmt->bind_result($id,$name);$mysqli_stmt->execute();while($mysqli_stmt->fetch()){echo $id.'--'.$name;}$mysqli_stmt->close();$mysqli->close();?>



mysql先行編譯處理(mysqli、PDO)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.