0

I am getting an error in num_rows property in php mysql

My code:-

<?php 

require ('connect.php');

$inputemail = $_POST['email'];

$sql =  "SELECT*FROM accounts WHERE Email='$inputemail' ";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

  die ("Email is already registered....");

}

else {

  echo "Congrats!!";

}


?>

Error:-

Notice: Trying to get property 'num_rows' of non-object

But this is working:-

<?php 

require ('connect.php');

$inputemail = $_POST['email'];

$sql =  "SELECT*FROM accounts ";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

  die ("Email is already registered....");

}

else {

  echo "Congrats!!";

}


?>

Why??

  • Is your query running? I'm wondering if the lack of spaces in `SELECT*FROM` is causing a syntax error. Does it run in phpmyadmin in that same form? In any case you need to use prepared statements instead of concatenating strings into your query, once you have it working. – droopsnoot Apr 06 '21 at 08:06
  • See [how can I prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and then fix the gaping security hole in your code. – ADyson Apr 06 '21 at 08:08
  • @droopsnoot I have tried your way but it is not working – Harshil Maheshwari Apr 06 '21 at 08:19
  • Thanks for the update. Something is wrong with your WHERE clause then. e.g. it could be an issue with the input data (I could break your query by simply putting an apostrophe (`'`) in the email field, or with a SQL injection attack) or it could be the field name is wrong, or something else even. You need to get the actual error message from mysqli - it's not hard to do, please see the duplicate question in the blue box. You also need to fix the SQL injection / quote-mark issue anyway. – ADyson Apr 06 '21 at 08:39

0 Answers0