Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXISTS always returns true simple query

I use this code to check if an entry already exsists but it seems like the query always returns true. When i use the same query on shell it works properly. Have in mind that foodname is Primary Key.

$query = "SELECT EXISTS ( SELECT * FROM Food WHERE foodname = '$food_name')";
    $result = pg_query($conn,$query) or die("Query could not be executed");
    if($result)
    {
        echo 'food already exists: ';
        echo $food_name;
        printf("\n");
    }
    else
    {
        echo 'new food inserted';
        printf("\n");
        $query = "INSERT INTO food VALUES ('$food_name','$food_price','$food_date')";
        $result = pg_query($conn,$query) or die("Query could not be executed");
    }

QUESTION: I modified it just like 'download download' said and it works as it also works with Kettners answer but isn't EXISTS faster for checking if an entry already exists for the reason that it stops when it finds a pair? Is there any query using EXISTS that can work in this case? Thnx for the help.

ANSWER: After reading everything you guys said the following one is what i choosed to use,it works and also uses EXISTS.

$query = "SELECT 1 FROM food WHERE EXISTS ( SELECT * FROM Food WHERE foodname = '$food_name')";
        $result = pg_query($conn,$query) or die("Query could not be executed");
        $row = pg_fetch_row($result);
        if($row[0])
        {
            echo 'food already exists: ';
            echo $food_name;
            printf("\n");
        }
        else
        {
            echo 'new food inserted';
            printf("\n");
            $query = "INSERT INTO food VALUES ('$food_name','$food_price','$food_date')";
            $result = pg_query($conn,$query) or die("Query could not be executed");
        }
like image 467
prof chaos Avatar asked Oct 19 '25 15:10

prof chaos


2 Answers

Just try this :

  $query="SELECT * FROM Food WHERE foodname = '$food_name'";
  $result = pg_query($conn,$query) or die("Query could not be executed");

  if(pg_num_rows($result )>=1){
     echo 'food already exists: ';
     echo $food_name;
     printf("\n");
  }
else{
        echo 'new food inserted';
        printf("\n");
        $query = "INSERT INTO food VALUES ('$food_name','$food_price','$food_date')";
        $result = pg_query($conn,$query) or die("Query could not be executed");
}
like image 118
user2460074 Avatar answered Oct 21 '25 05:10

user2460074


Database : postgresql
Query : giving result on success t and on fail f
Type : boolean.

You have to change your if condition.

if($result == 't'){
   // Your code here
}else {
  // Your code here
}

Modified :

$result = pg_query($conn, "Your Query");
$rows = pg_num_rows($result);
if($rows != -1){
   // Success
}else {
   // Fail
}
like image 25
Monty Avatar answered Oct 21 '25 04:10

Monty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!