phpmysql-num-rows

num_rows always gives back that there are no rows (that are equal to my input) in the database which isn't true


I'm trying to find out if the title someone filled in in a form is already in the database (cause otherwise I'm going to add a number to the title). But somehow the code underneath always give back "doesn't exists".

[my_table_name] is of course changed by the correct table name. Anyone knows what can be the problem?

$title = $_POST["title"]; 
$sql = "SELECT * FROM [my_table_name] WHERE title=$title";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>0) {
  echo "exists";
} else {
  echo "doesn't exist";
}

Solution

  • Your SQL. Add single quotes on $title:

    $sql = "SELECT * FROM [my_table_name] WHERE title='$title'";