I know this question has been asked before but it seems like the solutions have been specific to the problem presented.
I have a codebase with hundreds of instances where mssql_num_rows is used.
Code example:
$db->execute($sql);
if ($db->getRowsAffected() > 0) {
$total = $db->fetch();
In db class:
$this->rowsaffected = mssql_num_rows($this->query_result);
- I can't create generic
SELECT count(*) FROM table
queries as I have too many specific select statements. - I could run a
preg_replace
to remove everything betweenSELECT and FROM
and then replace with aCOUNT(*)
and run a second query but this assumes all queries are setup a certain way. - I could
fetchAll
first thencount()
the results but that means upgrading all instances of the if statements.
So what is the best all around replacement to a *_num_rows function if people are updating their code to PDO. Not something that solves a specific problem, but something that replaces the functionality of *_num_rows. If that's not possible what allowed it to be possible before?
If you want to count the rows you can do this with PDO:
There is no way to directly count rows when using a
SELECT
statement with PDO.So with everyone's help this is what I built.
$this->rowsaffected is already being updated in the execute phase if the statement is an INSERT, UPDATE, or DELETE with $sth->rowCount() so I only needed to run this second query on SELECT and SHOWS.
I feel bad though, because just as I mentioned in my question, that I was looking for an overall solution I seem to have stumbled onto a specific solution that works for me since the code already asks for the number of rows using a function. If the code was doing this:
then this solution would still create work updating all instances to use that custom function.