[英]Update single field in base64 in whole table with PDO
I have a mySQL DB defined with one table that has lots of fields. 我有一个带有多个字段的表定义的mySQL DB。
Is there an all in one PDO statement I could execute to do the following : 我可以执行以下所有操作吗?
I want to base64_encode one field (field called cstat) in every single record in the DB. 我想对数据库中的每条记录中的一个字段(称为cstat)进行base64_encode编码。
Any help would be much appreciated. 任何帮助将非常感激。
UPDATE : 更新:
Thanks to TheEwook getting me on the right track and some further searching I finally solved it like this (I have a unique autoincrement field called record_id) : 多亏了TheEwook让我走上了正确的道路,并进行了进一步的搜索,终于使我像这样解决了它(我有一个独特的自动递增字段,称为record_id):
$sth = $pdo->query("SELECT * FROM myTable");
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()) {
$cstat = base64_encode($row['cstat']);
$sql="UPDATE myTable SET cstat = :cstat WHERE record_id = :record_id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cstat', $cstat, PDO::PARAM_STR);
$stmt->bindParam(':record_id', $row['record_id'], PDO::PARAM_INT);
$stmt->execute();
}
A simple update query will suffice 一个简单的更新查询就足够了
$sth = $dbh->prepare('UPDATE myTable SET cstat = TO_BASE64(cstat)');
$sth->execute();
Or if you are using a mySQL version prior to 5.6.1 或者,如果您使用的是5.6.1之前的mySQL版本
$sth = $dbh->query("SELECT * FROM myTable");
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()) {
$cstat = base64_encode($row['cstat']);
$sth2 = $dbh->prepare('UPDATE myTable SET cstat = ?');
$sth2->execute(array($cstat));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.