Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Add num rows functions #5

Closed
Codez1337 opened this issue Aug 16, 2013 · 5 comments
Closed

Add num rows functions #5

Codez1337 opened this issue Aug 16, 2013 · 5 comments

Comments

@Codez1337
Copy link

We need a num rows function added.

@entomb
Copy link
Owner

entomb commented Aug 19, 2013

Hello @Codez1337 ,

You can use $result->num_rows, it's a property rather than a method but it will hold how many rows the resultset has.

for affected_rows you have the $db->affected_rows() method and you should call right after your query.

example:

$result = $db->query("SELECT * FROM users");
echo $result->num_rows; //how many users


$db->query("DELETE from users WHERE active = 0");
echo $db->affected_rows() //how many rows were deleted;

@entomb entomb closed this as completed Aug 19, 2013
@Codez1337
Copy link
Author

a question which i must ask and may look stupid, but how do you use that in an if statement?

i want to count the rtows and if there are not enough then exit that part of the script. now i know that the old mysql way works but i am trying to get into using classes and oop so how would i do it with this?

I ask because i am getting errors trying to do it the old way.

@entomb
Copy link
Owner

entomb commented Aug 22, 2013

well its quite simple, properties work like variables, actually this num_rows is a direct property from the native php mysqli object.

so you can use it like this

if($result->num_rows==0){
//empty
}

if(!$result->num_rows){
//empty
}

or like this:

if($result->num_rows){
//not empty!
}

@Codez1337
Copy link
Author

so the var i name the query becomes an object?

@entomb
Copy link
Owner

entomb commented Aug 26, 2013

@Codez1337 , well, yes, it returns a resultset object that helps you access your data

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants