There are many conflicting statements around. What is the best way to row count using PDO in PHP? Before using PDO, I just simply used mysql_num_rows
.
fetchAll
is something I won't want because I may sometimes be dealing with large datasets, so not good for my use.
Do you have any suggestions?
To use variables within the query, you can
bindValue()
orbindParam()
To use variables within a query you have to usebindValue()
orbindParam()
. And do not concatenate the variables with" . $variable . "
GL
I tried
$count = $stmt->rowCount();
with Oracle 11.2 and it did not work. I decided to used a for loop as show below.when you make a COUNT(*) in your mysql statement like in
your mysql query is already counting the number of result why counting again in php? to get the result of your mysql
and
$nb
will contain the integer you have counted with your mysql statement a bit long to write but fast to executeEdit: sorry for the wrong post but as some example show query with count in, I was suggesting using the mysql result, but if you don't use the count in sql fetchAll() is efficient, if you save the result in a variable you won't loose a line.
count($table)
will return the number of row and you can still use the result after like$row = $table[0]
or using aforeach
This post is old but Getting row count in php with PDO is simple
When it is matter of mysql how to count or get how many rows in a table with PHP PDO I use this
credits goes to Mike @ codeofaninja.com
You can combine the best method into one line or function, and have the new query auto-generated for you: