简体   繁体   English

为什么这个mysql查询不起作用?

[英]Why won't this mysql query work?

I have this query: 我有这个查询:

$q = $dbc -> prepare("SELECT * FROM mailbox WHERE msgTo = ? && read = 0");
$q -> execute(array($user['id']));
$mailboxCount = $q -> rowCount();

Now in my table I have 现在在我的桌子上

msgTo = 1, read = 0

$user['id'] is equal to 1 $ user ['id']等于1

But the row count returns 0 why is this? 但是行计数返回0,这是为什么呢?

from Manual Manual

PDOStatement->rowCount — Returns the number of rows affected by the last SQL statement PDOStatement-> rowCount —返回上一条SQL语句影响的行数

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object. PDOStatement :: rowCount()返回受相应PDOStatement对象执行的最后一个DELETE,INSERT或UPDATE语句影响的行数。

SEE this example and written clearly 看这个example并写清楚

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. 对于大多数数据库,PDOStatement :: rowCount()不会返回受SELECT语句影响的行数。 Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. 而是使用PDO :: query()发出与所要SELECT语句相同的谓词的SELECT COUNT(*)语句,然后使用PDOStatement :: fetchColumn()检索将返回的行数。

IMHO rowCount() only matches on UPDATE and DELETE statements and not on SELECT queries. IMHO rowCount()仅在UPDATE和DELETE语句上匹配,而在SELECT查询上不匹配。 By selecting * you fetch all data from the database. 通过选择*,您将从数据库中获取所有数据。 Since you only want the number of rows, use the count()-function instead - that is much more efficient. 由于只需要行数,因此请改用count()函数-这样效率更高。

SELECT count(*) FROM mailbox WHERE msgTo = ? && read = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM