我使用pdo连接来访问sybase数据库。
当使用rowCount时,结果是-1
$stmt = $dbh->prepare("select .... FROM users u WHERE u.ds_username like '%user%' order by ds_username ASC");
$stmt->execute();
$count = $stmt->rowCount(); 发布于 2017-09-22 12:17:52
根据rowCount文档,
PDOStatement::rowCount()返回受相应PDOStatement对象执行的上次DELETE、INSERT或UPDATE语句影响的行数。 如果关联的PDOStatement执行的最后一条SQL语句是SELECT语句,则某些数据库可能返回该语句返回的行数。然而,并不是所有数据库都有这种行为的保证,便携式应用程序也不应依赖这种行为。
和
对于大多数数据库,PDOStatement::rowCount()不返回受SELECT语句影响的行数。相反,使用PDO::query()发出一个SELECT (*)语句,其谓词与预期的SELECT语句相同,然后使用PDOStatement::fetchColumn()检索将要返回的行数。
示例
<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* No rows matched -- do something else */
else {
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>https://stackoverflow.com/questions/46364571
复制相似问题