如何将int数组添加到变量中

时间:2015-08-31 13:45:07

标签: php mysql pdo

if (_.has(usr, 'info.age') && _.has(usr, 'info.addr1') && _.has(usr, 'info.addr2') && _.has(usr, 'info.zipCode')) {
    // do your stuff since all criteria were met
} 
$pagename = 'index.html';
$dbh = connectDb();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query("select 'tcount' from accessNum where pagename = '$pagename'");
$record = $stmt->fetchAll();

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update accessNum set tcount = '$counter' where pagename = '$pagename'");
}

每次更新时,tcount都会保留1。为什么会发生?

3 个答案:

答案 0 :(得分:0)

如果你使用int,你就不需要像这样的变量

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update accessNum set tcount = $counter where pagename = '$pagename'");
}

答案 1 :(得分:0)

SELECT查询

中的列名称周围不需要引号
$stmt = $dbh->query("select tcount from accessNum where pagename = '$pagename'");

此外,UPDATE查询中的数字周围不需要引号

$dbh->exec("update accessNum set tcount = $counter where pagename = '$pagename'");

答案 2 :(得分:0)

始终使用反引号转义列和表名,并使用引号转义值。他们在MySQL中完全不同。这应该有效:

$pagename = 'index.html';
$dbh = connectDb();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query("select `tcount` from `accessNum` where `pagename` = '$pagename'");
$record = $stmt->fetchAll();

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update `accessNum` set `tcount` = '$counter' where `pagename` = '$pagename'");
}