-1

i'm trying to use like in mysql from php and i write this code

$query = mysqli_query($connection, "SELECT * FROM items order by create_at desc where content LIKE '" . $content . "%'")
  or die(mysqli_error($connection));

but it says there is an error in my syntax it says like this

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where content LIKE 'c%'' at line 1

1
  • 1
    problem in ordering, SELECT * FROM TABLE WHERE CONDITION ORDER LIMIT. you should follow mysql order of statements. Commented Jan 9, 2019 at 7:02

3 Answers 3

4

Order BY clause always come in the end, use WHERE clause first and then use ORDER BY

Solution would be this :

SELECT * FROM items where content LIKE 'yourVariable%' ORDER BY create_at DESC 
Sign up to request clarification or add additional context in comments.

2 Comments

Logically speaking , we get data first and then sort it later!
...except LIMIT
1

Your PHP code should be:

$query  = "SELECT * FROM items  where content LIKE '" . $content . "%' order by create_at desc";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));

Query clauses sequence should be :

  • Select
  • From
  • join
  • where
  • group by
  • order by
  • limit

Comments

0
$query = mysqli_query($connection, "SELECT * FROM items order by create_at desc where content LIKE '%.$content.%'") or die(mysqli_error($connection));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.