1

I am having trouble as to why the statement below won't do as I expect. Below is just a snippet and i am trying to compare the two post variables to the database table...

   if ($_SERVER['REQUEST_METHOD'] == 'POST'){
       $user_databaseF = $_POST['firstname_'] ;
       $user_databaseS = $_POST['lastname_'];
       $table_check_query = "SELECT * FROM clients_table WHERE _first = '$user_databaseF' AND _surname = '$user_databaseS'";
       $result_of_table_query =  $mysqli->query($table_check_query);
    if(empty($result_of_table_query)) {
       echo " we are good to start" ;    // Im getting duplicates in table

I'm thinking its here if(empty($result_of_table_query))

1
  • weird... im assuming the results are always empty... dont know why Commented May 2, 2015 at 18:55

2 Answers 2

1

empty function checks if variable is empty. But mysqli::query returns either a boolean true/false, or a mysqli_result object.

Your comparison empty($result_of_table_query) will be true only if mysqli::query return false. This will happen in case of query error. Empty result is not an error. I advise you to do this:

 $table_check_query = "SELECT count(*) as `count` FROM clients_table WHERE _first = '$user_databaseF' AND _surname = '$user_databaseS'";
 $db_result =  $mysqli->query($table_check_query);
 // as a result of a query you will have a mysqli_result object
 // to get values in a result use for example `fetch_array`
 $result = $db_result->fetch_array();
 // print_r $result to check what you have.
 // if all is cool there will be an array with key `count`
 // which will contain number of records. Check it:
 if ($result['count'] == 0) { /* you have no duplicates */ }
Sign up to request clarification or add additional context in comments.

Comments

1

empty returns TRUE for a value that is empty or evaluates as FALSE. An empty mysqli_result is neither.

Instead, you can use mysqli_num_rows to check how many results the query returns:

$result_of_table_query =  $mysqli->query($table_check_query);
if(mysqli_num_rows($result_of_table_query) == 0) {
    echo "we are good to start"; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.