Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see if result of SQL query is empty before performing other queries in PHP

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);
like image 829
Marko Vidalis Avatar asked May 25 '15 03:05

Marko Vidalis


People also ask

How do you check if SQL result is empty in PHP?

The easiest way is to use mysql_num_rows(). $cnt = mysql_num_rows($Result); if ( 0===$cnt ) { echo 'no records'; } else { while( false!=( $row=mysql_fetch_array($Result)) ) { // ... } }

How do I check if a field is empty in SQL?

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.

Is null in PHP MySQL?

A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value. Note: A NULL value is different from a zero value or a field that contains spaces.


1 Answers

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.

like image 101
Anant Kumar Singh Avatar answered Oct 07 '22 15:10

Anant Kumar Singh