如何在PHP中执行其他查询之前查看SQL查询的结果是否为空

I have the following PHP code which is for a voting system of an app. Its a Q&A app, and the user can vote for questions and answers that are posted.

In my php code, I first check if the user has voted for a specific question. This would exist in the QVOTES table, with the email and the id of the question being voted for.

When performing this check, I am not sure of how to see if $result is an empty set, so as to submit the user's vote if they have not voted for the question yet.

How can i get this working? All help is greatly appreciated.

<?php

$con=mysqli_connect("127.2.1.1","S837","887","D887");
if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qid = $_POST['qid'];
$email = $_POST['email'];
$result = mysqli_query($con, "SELECT * FROM QVOTES WHERE QID = $qid AND EMAIL = '$email'");
if (!mysqli_num_rows($result) ){
    if ($result = mysqli_query($con, "INSERT INTO QVOTES (QID, EMAIL) VALUES     ($qid, '$email')")) {
            mysqli_query($con, "Update QUESTIONS SET VOTES = VOTES +1 WHERE     QID = $qid");
            echo "Update successful";
        } else{
            echo "Update unsuccessful";
        }
            } else{
        echo "null";        
    }
    mysqli_close($con);

Actually you are doing in a wrong way. Please try to do like this:-

<?php

$con=mysqli_connect("127.2.1.1","S837","887","D887");
if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qid = $_POST['qid'];
$email = $_POST['email'];
$result = mysqli_query($con, "SELECT * FROM QVOTES WHERE QID = $qid AND EMAIL = $email") or die(mysqli_error($con)); // no need of extra quote
if ($result->num_rows == 0 ){ // means no vote-up done till now
    $result = mysqli_query($con, "INSERT INTO QVOTES (QID, EMAIL) VALUES ($qid, $email)")or die(mysqli_error($con)); // insert
    if($result){
        echo "Vote Added successfully.";
    } else{
       echo "Error occur while adding vote.Please try again.";
    }
} else{
    $result = mysqli_query($con, "Update QUESTIONS SET VOTES = VOTES +1 WHERE  QID = $qid AND EMAIL = $email")or die(mysqli_error($con)); // upddate
       if($result){
        echo "Vote updated successfully.";
    } else{
       echo "Error occur while updating vote.Please try again.";
    }
}
    mysqli_close($con);

Note:- I change message for better understanding. You can change according to your wish. thanks.

How to see if $result is an empty set?

From the docs:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE (Ref)

Use $result->num_rows if $result is not FALSE;