Sima
Sima

Reputation: 13

IF-ELSE from MySQL based on single row

I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?

<?php

include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];

$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);

if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
 $output="maybe";
else{
 $output="error";
}
print(json_encode($output));
mysql_close();

?>

Upvotes: 1

Views: 39

Answers (2)

Piyush Bansal
Piyush Bansal

Reputation: 1723

You are not processing your results. You just get the total number of rows in $row variable.

You need to process your query result by looping method.

Here is the example

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
    if($row['entryScans'] = 0)
    {
      $output="ok";
    }
    else if ($row['entryScans'] > 0) 
    {
      $output="maybe";
    }
    else
    {
    $output="error";
    }
}

Hope this works for you.

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

You are doing ...

if($row['entryScans'] = 0){

Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...

$result = mysql_query($sql);
$row=mysql_num_rows($result);

So $row is the number of rows, $result is the query results...

So to check the number of rows in the result set

if($row == 0){

etc.

Update:

If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so

$row=mysql_fetch_assoc($result);

Then you can leave the rest of the code to use $row['entryScans']

Upvotes: 2

Related Questions