Jump to content
Sign in to follow this  
westman

numRows

Recommended Posts

My code is not working, here it is...

$stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$numRows = $stmt->num_rows;
$stmt->close();
if($numRows > 0) {	
$error = "Your Email is already in use. Please try another.";
$conn->close();
}else{
// all good
$conn->close();
}

any help?

Share this post


Link to post
Share on other sites

What does "not working" mean? Are you getting any error messages?

Share this post


Link to post
Share on other sites

It will not show me the error message.

There is more than one of the same email in the data base.

Edited by westman

Share this post


Link to post
Share on other sites

Have you verified that the variable $email contains the correct value?

Share this post


Link to post
Share on other sites

You need to add some debugging statements. You have to try to find out what your code is doing and why it's not working properly.

// Are we getting the correct e-mail address?
var_dump($email);

$stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
if(!$stmt) {
  // Was the query valid SQL?
  echo 'SQL error: ' . $stmt->error . '<br>';
}

$stmt->bind_param("s", $email);
$success = $stmt->execute();
if(!$success) {
  // Did the query execute properly?
  echo 'MySQL error: ' . $conn->error . '<br>';
}

$numRows = $stmt->num_rows;

// Is this a number or is it null?
var_dump($numRows);


$stmt->close();
if($numRows > 0) {	
$error = "Your Email is already in use. Please try another.";
$conn->close();
}else{
// all good
$conn->close();
}

 

Share this post


Link to post
Share on other sites

Is that PDO?  Because numrows doesn't always return the number of rows in a select query.  You should use a count query instead.

Share this post


Link to post
Share on other sites

I was under the impression it was MySQLi, which does use num_rows for SELECT statements.

Share this post


Link to post
Share on other sites

From the manual:

The use of mysqli_stmt_num_rows() depends on whether or not you used mysqli_stmt_store_result() to buffer the entire result set in the statement handle.

The examples in the manual do show using store_result first.

Share this post


Link to post
Share on other sites

The manual is at php.net, you really need to know where it is to avoid having to wait 6 hours for someone to give you a link to it.  There's a search bar on the top right where you can type any function name.

http://php.net/manual/en/

Share this post


Link to post
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
Sign in to follow this  

×