-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database getNumRows() #109
Conversation
$mysql = \Config\Database::connect('mysql', true); $num_rows = $mysql->query("SELECT * FROM teste")->numRows();
Is this a proposed enhancement to ResultInterface? If so, that would be appropriate as a description. Also, if that is the case, it would need changes in ResultInterface, along with the related user guide and unit testing changes. Also, it should follow the style of the other interface methods, namely be called getNumRows. |
I have merged the other branch please close other PR please. can you explain me how unit testing works please?? |
I had left the numRows out on purpose. We can always discuss whether to provide it or not but we had been discouraging its use for the last couple of years and writing it out of the user guide examples due to performance and memory issues on several platforms. |
and how validate if row with certain id exists?? |
All getResult methods will return an empty array and all getRow methods return null if the record doesn't exist.
|
make sense 👍 |
Since I don't see any discussion about whether we need this or not I'm going to assume that we're good, and close it. |
I suspect that the vast majority of CodeIgniter projects use one of the DBMS modules that have a num_rows function (MySQLi, Postgres, MSSQL). I strongly believe that CodeIgniter 4 should expose a Result->numRows method for a variety of reasons which I've enumerated on the CodeIgniter forum. I would add to these points the fact that my existing CI3 project uses query->num_rows in no less than a hundred places and that this function was attached not to a query build or db class, but to the query result object. I hope that the CI4 devs will consider the difficulty of CI3 -> CI4 migration and add this very convenient and efficient function for the popular DBMSes that support it. EDIT: it also occurred to me that there is no countAllResults function available if one just used db->query or db->simpleQuery instead of the QueryBuilder. Please restore this vital function to the database Result classes that natively support a num_rows function: MySQLi, Postgres, SQLSRV. My post from the CI forums follows:
|
We would happily accept a PR for this if you'd care to implement it. |
@lonnieezell should I fork the develop branch? Or some other branch? EDIT: I see here that pull requests should use the develop branch:
|
The number of rows returned by the query. Make sure to call the method using your query result object:
$query = $db->query('SELECT * FROM my_table');
echo $query->getNumRows();