-1

how can i use right code for using ORDER BY id DESC and WHERE

$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']."ORDER BY id DESC";
Hamad
  • 5,096
  • 13
  • 37
  • 65
  • Try adding space before `ORDER BY` – rs. Dec 20 '13 at 18:35
  • Welcome to StackOverflow! Without sounding rude, this question could have been avoided with a little patience and research. If you haven't managed to find the problem after hours of debugging, a quick Google search **"mysql where order by"** would have yielded hundreds of examples, tutorials and explanations. – TheCarver Dec 20 '13 at 19:08
  • http://stackoverflow.com/questions/13620961/order-by-id-desc?rq=1 – TheCarver Dec 20 '13 at 19:19

4 Answers4

2

Add a space here as shown.

$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
                                                                ----^

Also, don't pass your parameters like $_GET or $_POST directly into the SQL query as it will definitely lead to SQL Injection Attacks. Filter those parameters or make use of Prepared Statements.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 1
    Cannot see a reason for the down-votes. In all fairness, it should be getting up-voted, as it's the only answer that has warned against injection attacks and suggests the use of prepared statements. – TheCarver Dec 20 '13 at 19:24
0

add a space before 'ORDER' at least

 $sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
blotto
  • 3,387
  • 1
  • 19
  • 19
0

You need provide space before ORDER BY

$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Simply Use this :

$sel = "SELECT * FROM items where portfolio_id ='$_GET[folio_id]' ORDER BY id DESC";
Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92