How to sort with a mysql column

Hi EE.

I have a query that retrieve data that has the provided word and this works well but now I want to sort this data when being retrieved based on a column name `lp` but I don't seem to be able to do so as if I add the Order by then the query seems not to return anything as the row is returned with 0. Below is the query code that I'm referring to here.
$querrry = "SELECT * FROM `xxxxdatabase`.`xxxxtable`  WHERE tags LIKE '%$searchq%' OR name LIKE '%$searchq%' OR category LIKE '%$searchq%' OR username LIKE '%$searchq%' OR addr2 LIKE '%$searchq%' OR city LIKE '%$searchq%' OR province LIKE '%$searchq' OR country LIKE '%$searchq%' OR company LIKE '%$searchq%' LIMIT 0,10 ORDER BY lp ASC " or die(mysqli_error());
$resultz = mysqli_query($conn, $querrry);

Open in new window


I'm using Mysqli.
LVL 2
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter SchultzSenior Network AdministratorCommented:
hmm is that field Ip numeric ? Then try IsNum ... ORDER BY lp IsNum " or ....
0
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
I've just tried that but still the row is returned with 0 which means there was no search.
0
Chris StanyonWebDevCommented:
The 0 value indicates a failure (false) and not zero records. You have an error in your SQL. You need the ORDER BY before the LIMIT:

SELECT * FROM `xxxxdatabase`.`xxxxtable`  WHERE tags LIKE '%$searchq%' OR name LIKE '%$searchq%' OR category LIKE '%$searchq%' OR username LIKE '%$searchq%' OR addr2 LIKE '%$searchq%' OR city LIKE '%$searchq%' OR province LIKE '%$searchq' OR country LIKE '%$searchq%' OR company LIKE '%$searchq%' ORDER BY lp ASC LIMIT 0,10

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Chris is right, MySQL is very picky about the order of the statements in a query.
0
NerdsOfTechTechnology ScientistCommented:
LIMIT should be last in the sequence,

Also you were missing a % in the province LIKE

$querrry = "SELECT * FROM `xxxxdatabase`.`xxxxtable`  WHERE tags LIKE '%$searchq%' OR name LIKE '%$searchq%' OR category LIKE '%$searchq%' OR username LIKE '%$searchq%' OR addr2 LIKE '%$searchq%' OR city LIKE '%$searchq%' OR province LIKE '%$searchq%' OR country LIKE '%$searchq%' OR company LIKE '%$searchq%' ORDER BY lp ASC LIMIT 0,10" or die(mysqli_error());
$resultz = mysqli_query($conn, $querrry);

Open in new window


Last but not least, make sure $searchq is 'sanitized' to prevent an SQL injection attack. Specifically, because you are using LIKE you will want to filter the input beyond real_escape_string() and remove % and _ characters as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.