Please help me learn what is wrong with this PHP / MySQL query.
This is my channels table:
name url id key
BBC2 http://url.com/2 BBC2 2
BBC1 http://url.com/1 BBC1 1
This works:
$getchans = "SELECT * FROM channels";
$channels = mysqli_query($db,$getchans);
foreach ($channels as $channel)
{
// do stuff
}
This does not work:
$getchans = "SELECT * FROM channels ORDER BY key ASC";
$channels = mysqli_query($db,$getchans);
foreach ($channels as $channel)
{
// do stuff
}
and gives error Warning: Invalid argument supplied for foreach()
Can anyone please tell me why it doesn't work when MySQL query/result is ordered?
Thank you.
esacepe关键字关键字, 请参阅
$getchans="SELECT * FROM channels ORDER BY `key` ASC";
key is a keyword in mysql so you need to use `` quotes in your query.
$getchans="SELECT * FROM channels ORDER BY key
ASC";
Your query failed because key
is a reserved keyword in SQL. Try to change the name of the column key
to something else. There is also a very usefull list with all the reseverd keywords.
If you don't prefer an other name for this column, you can try to escape it by using the following query:
SELECT * FROM channels ORDER BY `key` ASC
List with reserved keywords can be found on https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Goodluck!
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.