<!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 { $pdo = new PDO("mysql:dbname=mydb;host=localhost","root","123"); } catch(PDOException $e) { echo "连接出错".$e->getMessage(); } //方法1 $sql = "insert into info values(?,?,?,?)"; //准备SQL语句 $st = $pdo->prepare($sql); //执行SQL语句,简便方法 $st->execute(array('','maliu','333','n04')); //按顺序添加 //方法2 $sql = "insert into pinpai values(:Code,:Name,:First)"; //冒号是固定格式,冒号+列名 $st = $pdo->prepare($sql); $st->execute(array('Code'=>'p99','Name'=>'吉普','First'=>'J')); //参数是关联数组, $_POST或$_GET可直接使用 ?> ?> </body> </html>