-2

Trying to select some specific arrays from my mysql database, I used this:

 $result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` =< '$p2'  ORDER BY id DESC") or die(mysql_error());

$p1 and $p2 are multiples of 5. for example: 0 and 5, 5 and 10, 10 and 15; but using this code, I faced this error not knowing how to fix it!

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=< '15' ORDER BY id DESC' at line 1

Thank you for guiding me.

  • 7
    `=<` is an invalid operator in PHP and in SQL (and indeed in most programming languages); you mean `<=` – Mark Baker Aug 10 '15 at 08:14
  • Use `BETWEEN` in your SQL statement instead of defining the range using two value limitations, as seen here: [link](http://stackoverflow.com/questions/1630239/sql-between-vs-and) Also make sure your PHP-vars are sanitized before just pasting in your SQL-statement, because of SQL-injection hazard. – klaar Aug 10 '15 at 08:25

6 Answers6

0

Try:

 $result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` <= '$p2'  ORDER BY id DESC") or die(mysql_error());

The operator =< doesn't exist in SQL.

Also, you can see a list of all the comparison operators here: http://docs.oracle.com/html/A95915_01/sqopr.htm#i1004774

barbarity
  • 2,420
  • 1
  • 21
  • 29
0

Like Mark Baker stated in his comment to you it is an invalid operator in PHP

Change this

$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` =< '$p2'  ORDER BY id DESC") or die(mysql_error());

To this

$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` <= '$p2'  ORDER BY id DESC") or die(mysql_error());
0

replace your query wit this..

$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` <= '$p2'  ORDER BY id DESC") or die(mysql_error());
Manoj Salvi
  • 2,639
  • 1
  • 17
  • 21
0

Try this

$result2 = mysql_query("SELECT * FROM books WHERE  id >= '$p1' AND id <= '$p2'  ORDER BY id DESC") or die(mysql_error());

Don't use ` in php code

And =< should define as <=.

Improvements

use SELECT * without giving all database fields

If I copy your code to my IDE, it shows all the syntax Errors

enter image description here

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Problem remains on your less than or equal operator. It should be (<=). So your query will be:

$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` >= '$p1' AND `id` <= '$p2'  ORDER BY id DESC") or die(mysql_error());

Alternately you can use BETWEEN for range selection:

$result2 = mysql_query("SELECT `id`,`title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` FROM `books` WHERE  `id` BETWEEN '$p1' AND '$p2'  ORDER BY id DESC") or die(mysql_error());
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

As others have stated the =< operator is incorrect and that is likely the cause of the error but the search of less than / greater than can be simplified using 'between'

$sql="select 
            `id`, `title`, `author`, `publication`, `series`, `price`, `image`, `description`, `quantity`, `status` 
                from `books` 
                where  `id` between $p1 and $p2 
                order by id desc;";

$result2 = mysql_query( $sql ) or die( mysql_error() );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46