如果你使用了最新版的XAMPP,那么你几乎不用改变php.ini的设置,就可以使用pdo
but,插了一晚上,程序既不报错也不插入数据,真是气死人,后来发现是实例化pdo对象的时候没有指定字符集。所以一定设定字符集属性,否则极有可能插入数据失败
- 一个插入多条数据的例子(用到了事务)
<?php
$dbtype = 'mysql';
$dbname = 'olddream';
$user = "root";
$pwd = "wenwajiao";
$ip='127.0.0.1';
$fuckingcharset = 'utf8';
$dsn = "$dbtype:host=$ip;dbname=$dbname;charset=$fuckingcharset";
$pdo = new PDO($dsn,$user,$pwd);
$rows = $pdo->prepare("select userid,username from userlist");
$rows->execute();
$pdo->beginTransaction();
$ret = $rows->fetchAll();
//update rbac_user set mypass = ? where userid = ?
for($i=0;$i<count($ret);$i++)
{
$sql = "insert into rbac_user (userid,username,mypass) values (?,?,?)";
$statement=$pdo->prepare($sql);
$statement->bindValue(1,$ret[$i]['userid']);
$statement->bindValue(2,$ret[$i]['username']);
$statement->bindValue(3,md5($ret[$i]['userid']));
$statement->execute();
}
$pdo->commit();
$pdo=null;
- 插入一条数据
<?php
$dbtype='mysql';
$db = 'olddream';
$ip='127.0.0.1';
$user='root';
$pwd = 'wenwajiao';
$charset = 'utf8';
$dsn = "$dbtype:host=$ip;dbname=$db;charset=$charset";
$pdo=new PDO($dsn,$user,$pwd);
$sql = "insert into rbac_user (userid,username,mypass) values (?,?,?)";
$statement=$pdo->prepare($sql);
$userid = 'masterzhang';
$username = '张教练';
$pwd = md5('masterzhang');
$statement->bindValue(1,$userid,PDO::PARAM_STR);
$statement->bindvalue(2,$username,PDO::PARAM_STR);
$statement->bindvalue(3,$pwd,PDO::PARAM_STR);
$statement->execute();
?>