Pdo's Rowcount() Not Working on PHP 5.2.6+

Work-around for PHP5's PDO rowCount MySQL issue

You can issue a SELECT FOUND_ROWS() query right after the original SELECT query to get row count.

$pdo->query("SELECT * FROM users");
$foundRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();

See also: MySQL Docs on FOUND_ROWS()

mySQL SELECT PDOStatement-rowCount

Yes, you should still care about the usage of that method. It's not just the database, but PHP itself can be a problem.

If you want to play it safe, it's better if you just issue a SELECT COUNT(*) instead.

How to make sure delete query removes a row?

With the help of rowCount();

PDOStatement::rowCount() returns the number of rows affected by the
last DELETE, INSERT, or UPDATE statement executed by the corresponding
PDOStatement object.

if ( $stmt->rowCount() ) {
// activating the user
} else {
// your token is invalid
}

php pdo fetch not working with functions

This is because of the scope of the mainMenu() function that variables outside are not available inside, you need to pass variables as parameters such as:

mainMenu($q);

Please see more about Variable Scope

Also, mysql_real_escape_string() is not required when outputting data.

Cannot set current _timestamp or now() with PDO on MySQL

It sounds like your problem might be that you are trying to use a MySQL function as a parameter value in the prepared query. This fails because the process of preparing the query will turn it into a literal for the SQL statement, including things like quoting strings, which is not what you want. So for setting a field to NOW(), do the assignment in the query itself before you make it prepared, not in any of the parameters.

On the other hand, using a null parameter value should work. But clearly it doesn't, which may be the bug you mentioned. Again, the solution is to do it directly in the SQL. You can still use parameters for other fields.

SQL_CALC_FOUND_ROWS / FOUND_ROWS() does not work in PHP

Thank you.

When I ran something analogous to your example on the mysql command line, it would work; but running it from php, it failed. The second query has to "know about" the first one, so I figure somehow that persistence/memory linking the two queries was getting messed up by the php.

(It turns out that Wordpress uses this type of query to do its pagination - so our larger problem was that the pagination in a wordpress install suddenly stopped working when we moved to php 5.2.6 ... eventually tracked it down to the FOUND_ROWS()).

Just for the sake of posting for people who may run into this in the future... for me it was the php setting "mysql.trace_mode" - this defaulted "on" in 5.2.6 instead of "off" like previously, and for some reason prevents the FOUND_ROWS() from working.

As a "fix", we could either put this in every php page (actually, in a common "include"):

ini_set("mysql.trace_mode", "0");

or add this to the .htaccess:

php_value mysql.trace_mode "0"

Thanks again,
Jerry

SELECT execute([]) Parse error: syntax error, unexpected '[', expecting ')' in

I can reproduce this error with PHP 5.2. Documentation about PHP array's syntax states:

... As of PHP 5.4 you can also use the short array syntax, which
replaces array() with []. ...

What you can do in your case is to define an array using array():

<?php

$sqlStatement = "
SELECT *
FROM $table_results
WHERE title = ? AND id_category = ?
";

...
$stmt = $bdConection->prepare($sqlStatement);
$stmt->execute(array($title, $id_category));
echo $stmt->rowCount();
...

?>


Related Topics



Leave a reply



Submit