user2067403
user2067403

Reputation: 43

comparision operation Sql query with php

$num=$_POST['data'];
$no = (int) $num;
$sql = "select * from uploads where id > '$no'"

The above query not working properly.It is displaying the values below the no.I think the problem with conversion.somebody please help to solve this problem

Upvotes: 0

Views: 62

Answers (5)

Guffa
Guffa

Reputation: 700512

You have apostrophes around the value, so the values will be compared as strings, not numbers. The string value 10 for example is smaller than the string value 2.

Remove the apostrophes:

$sql = "select * from uploads where id > $no";

Upvotes: 1

pamil
pamil

Reputation: 980

You have Sno = ..., it should be $no = .... It's a typo.

Then, numbers in query doesn't require apostrophes, so don't use them in this context.

You also had $_post instead of $_POST - it's another issue, variables in PHP are case-sensitive.

Upvotes: 0

Abu Romaïssae
Abu Romaïssae

Reputation: 3911

Try this code instead:

if ( empty( $_POST['data'] ) ){
    // show error message
    echo "No data received";
    // use a default values
    $num = 0;
}
else 
    $num=$_POST['data'];

$no = intval($num);
$sql = "select * from uploads where id > $no";

Try to use intval instead of casting to int

Upvotes: 1

soyuka
soyuka

Reputation: 9105

$no = (int) $_POST['data']; //wrong variable declaration ?
$sql = "select * from uploads where id > $no";

Try this.

$_post replaced by $_POST

one variable instead of two

Upvotes: -1

GautamD31
GautamD31

Reputation: 28753

Try with this

$sql = "select * from uploads where id > ".$no;

and also put $_POST instead of $_post

Upvotes: 0

Related Questions