Jump to content

MySQLi query result (num_rows)


LittleJoe

Recommended Posts

I'm using object-oriented MySQLi prepared statements and I'm getting the hang of it but there's something I don't quite understand. When I use INSERT, UPDATE, REPLACE and DELETE I can use affected_rows to know if I was successful, that the query actually did something. However, when I want to see if anything was returned by my SELECT statement I turn to num_rows in hope that I can see how many rows were returned before I try to use the result as an array. The problem I have is I always get the following: Notice: Undefined property: mysqli::$num_rows in C:\UniServer\www\website\index.php on line 60 So, should I be using num_rows or something different? How do you guys recommend that I first check to see if the result variable has received something before I try to use it as an object? Here's some code that shows what I've been doing:

 <?php $connection = new mysqli("localhost", "root", "password", "website_database"); $result = $connection->prepare("SELECT * FROM `users` WHERE email = ? AND password = ? LIMIT 1"); $result->bind_param("ss", $email, $password); $result->execute(); if ($connection->num_rows > 0) {    // Do something meaningful if there was actually a row returned, otherwise don't do anything.    // I don't want to just go straight to using result with "$row = $result->fetch_row();"    // because I don't even know if there was a user with that email and password.} ?> 

Edited by LittleJoe
Link to comment
Share on other sites

num_rows is defined on $result, not $connection. $connection is a mysqli object, $result is a mysqli_result object. They have different methods and properties.
Thank you so much that worked just great. What had me confused was that when I use affected_rows it works on $connection and not $result so that seems to be opposite to how things work with num_rows.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...