简体   繁体   中英

PHP SQL: Order search query results by user input order

I've tried to look for a solution but can't seem to grasp the issue I have.

I have a search query with a "where clause" stating if a user inputs multiple words return the result.

I need the result returned in the same order searched.

Even if i just add the addition "ORDER BY DESC" an error is thrown "Trying to get property of non-object".

Here is my code:

$word = $_GET['word'];
$word3 = $_GET['word'];
$word = explode(";", $word);
$noOfWords = count($word);
$word2 = $word3;

if ($noOfWords == 1) {
    $searchString = " word_eng LIKE '" . $conn->escape_string($word3)  
        "%'";
} else {
    $searchString = $whereClause = "";
    foreach ($word as $entry) {
        $searchString .= " OR word_eng LIKE '" . $conn->escape_string($entry) . "' ORDER BY '" . $word2 . "' ";
    }
}

$whereClause = ($searchString != "") ? " WHERE " . preg_replace('/OR/', 
        '', $searchString, 1) : $whereClause;


$sql = "SELECT word_eng FROM words " . $whereClause . " LIMIT 17";

$result = $conn->query($sql);

if ($result->num_rows > 0) { 

    while($row = $result->fetch_assoc()) { 

        $row1 = $row["word_eng"];

        echo $row1;     


    }

There are a couple of problems with the way your trying to use ORDER BY. There should be only 1 order by clause in any SQL, you are adding it in for each word your adding. The second part is that it's expecting to order by a column name and your ordering it by the words your searching for.

With wanting to maintain the order of the terms and the order of the results, it would be necessary to use an order by clause with something like a case ( Can you add an if statement in ORDER BY? may help explain this).

$orderBy = "";

if ($noOfWords == 1) {
    $searchString = " word_eng LIKE '" . $conn->escape_string($word3) ."%'";
} else {
    $searchString = $whereClause = "";
    $orderBy = " order by case `word_eng` ";
    foreach ($word as $order=>$entry) {
        $searchString .= " OR word_eng LIKE '" . $conn->escape_string($entry) . "'";
        $orderBy .= " when '$entry' then $order ";
    }
    $orderBy .= " end ";
}

$whereClause = ($searchString != "") ? " WHERE " . preg_replace('/OR/',
    '', $searchString, 1) : $whereClause;


$sql = "SELECT word_eng FROM words " . $whereClause . " " .$orderBy." LIMIT 17";
if ($noOfWords == 1) {
    $searchString = " word_eng LIKE '" . $conn->escape_string($word3)  
    "%'";
} else {
    $searchString = $whereClause = "";
    foreach ($word as $entry) {
        $searchString .= " OR word_eng LIKE '" . $conn->escape_string($entry);
    }
    $searchString .= "' ORDER BY '" . $word2 . "' ";
}

I think you messed up with MySQL Query string in bellow line code.

$searchString .= " OR word_eng LIKE '" . $conn->escape_string($entry) . "' ORDER BY '" . $word2 . "' ";

Your Query is generating Something like

ORDER BY DESC

And OrderBy Query should be something like this

ORDER BY expression [ ASC | DESC ];

So you are missing the expression in query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM