插入数据库中相关信息

# 最简单的方式执行(exec返回受影响行数)
$dbh = new PDO('mysql:dbname=book_base;host=192.168.1.137', 'root', 'root');
$dbh->exec("set names utf8");
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (1,'a123','1234567890','1234567890','测试昵称1',1506743300,1506743300,1506743300),
  (2,'a124','1234567890','1234567890','测试昵称2',1506743300,1506743300,1506743300),
  (3,'a125','1234567890','1234567890','测试昵称3',1506743300,1506743300,1506743300)
SQL;
$res = $dbh->exec($sql);
unset($dbh);

# 按序绑定参数执行(execute返回执行bool结果)
$dbh = new PDO('mysql:dbname=book_base;host=192.168.1.137', 'root', 'root');
$dbh->exec("set names utf8");
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (?,?,?,?,?,?,?,?)
SQL;
$sth = $dbh->prepare($sql);
$sth->bindValue(1, 4);
$sth->bindValue(2, 'a126');
$sth->bindValue(3, '1234567890');
$sth->bindValue(4, '1234567890');
$sth->bindValue(5, '测试昵称4');
$sth->bindValue(6, 1506743300);
$sth->bindValue(7, 1506743300);
$sth->bindValue(8, 1506743300);
$res = $sth->execute();
unset($sth);
unset($dbh);

# 按key绑定参数执行(execute返回执行bool结果)
$dbh = new PDO('mysql:dbname=book_base;host=192.168.1.137', 'root', 'root');
$dbh->exec("set names utf8");
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (:id,:user_name,:user_skey,:user_uuid,:user_nick,:free_time,:login_time,:insert_time)
SQL;
$id = 5;
$user_name = 'a127';
$user_skey = '1234567890';
$user_uuid = '1234567890';
$user_nick = '测试昵称5';
$free_time = 1506743300;
$login_time = 1506743300;
$insert_time = 1506743300;
$sth = $dbh->prepare($sql);
$sth->bindParam('id', $id);
$sth->bindParam('user_name', $user_name);
$sth->bindParam('user_skey', $user_skey);
$sth->bindParam('user_uuid', $user_uuid);
$sth->bindParam('user_nick', $user_nick);
$sth->bindParam('free_time', $free_time);
$sth->bindParam('login_time', $login_time);
$sth->bindParam('insert_time', $insert_time);
$res = $sth->execute();
unset($sth);
unset($dbh);
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""