更新数据库中相关信息

# 最简单的方式执行(exec返回受影响行数)
$dbh = new PDO('mysql:dbname=book_base;host=192.168.1.137', 'root', 'root');
$dbh->exec("set names utf8");
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = 1
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
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id IN(?,?,?)
SQL;
$sth = $dbh->prepare($sql);
$sth->bindValue(1, 10000);
$sth->bindValue(2, 10086);
$sth->bindValue(3, 95001);
$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
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$sth = $dbh->prepare($sql);
$sth->bindParam('id', $id);
$res = $sth->execute();
unset($sth);
unset($dbh);

results matching ""

    No results matching ""