dongnius85154 2014-04-29 16:30
浏览 17
已采纳

参赛作品将无法正确排序

I've got a 'contact' table in my database which stores the emails people are sending to me. In my adminpanel I'd like to order them by status. My contact table has an 'answered' INT column which updates as soon as I'm replying to them. Once I answered the email, the 'answered' will be set to '1' in the database.

As I said, I'd like to order the emails in my adminpanel by the 'answered' status. So all emails which arnt answered yet ('0' in database) should be on top and the emails I already have answered ('1' in database) should be on the bottom.

This is how I've currently got it:

$showTheEmails = array(); 
$getEmails = mysqli_query($mysqli,"SELECT * FROM contact ORDER BY 'answered' ASC") OR die (mysqli_error($mysqli));
if (mysqli_num_rows($getEmails) > 0){
    while($row = mysqli_fetch_array($getEmails)){
        $row = array( 
        'contact_id' => $row['contact_id'],
        'name' => $row['name'], 
        'email' => $row['email'], 
        'subject' => $row['subject'], 
        'timestamp' => $row['timestamp'], 
        'answered' => $row['answered'] );
        $showTheEmails[] = $row;
    }
}

And ofcourse the code what displays the array.

Problem is: it's not ordering correctly. What am I doing wrong? :(

  • 写回答

2条回答 默认 最新

  • douzi4766 2014-04-29 16:34
    关注

    By wrapping with single quotes, you've created the problem. You're instructing MySQL to try to order by the word "answered" (rather than the column in the database). Simply remove the quotes.

    SELECT * FROM contact ORDER BY answered ASC
    

    As pointed out in the comments, you may also surround the column names with `backticks` like

    SELECT * FROM `contact` ORDER BY `answered` ASC
    

    Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set (see below) It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.

    When to use single quotes, double quotes, and backticks in MySQL

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块