Juliver Galleto
Juliver Galleto

Reputation: 9037

trying to fetch result in ASC order but receive error

I have this code (refer below) to display result from mysql in ASC order

function showMenu(){
global $con; // SIMPLY ADD THIS LINE
$result = mysqli_query($con,"SELECT * FROM user_menu ORDER BY key ASC");
$menu = '<ul class="nav navbar-nav">';
while($row = mysqli_fetch_array($result))
{
$menu .= '<li class="' . $row['status'] . '"><a href="' . $row['url'] . '" class="extend"     title="' . $row['url'] . '">' . $row['name'] . '</a></li>';
}
$menu .= '</ul>';

return $menu;
}

but i got an error saying " Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in" and it point to line 38 and the line 38 is "while($row = mysqli_fetch_array($result))"

as you can see im trying to fetch result in ASC order by key and the key row contains numbers like, 0 1 2 3 4 so the result should be displayed as per number order.

how can i get raid of this error? ideas, suggestion and recommendations please let me know. Thanks in advance.

Upvotes: 0

Views: 53

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

Key is a reserved word in MySQL. You can see the list here.

That means that you need to escape it:

$result = mysqli_query($con,"SELECT * FROM user_menu ORDER BY `key` ASC");

If you are in control of the database, you should change the name of the column. Avoid using reserved words as identifiers.

Upvotes: 0

MagyaDEV
MagyaDEV

Reputation: 375

Try to escape key column name using backticks:

 SELECT * FROM user_menu ORDER BY `key` ASC;

Upvotes: 1

Related Questions