选择查询的顺序不在预准备语句中

Pau*_*aul 2 php mysql sql select prepared-statement

我创建了一个准备好的选择查询,看起来查询没有拿起DESC或我有bind_param结构错误.我id想要显示最后一个user_id的图像.显示用户的图像,但它是他们拥有的第一个id图像.我试过了ASC,这是同样的事情.

我这样做了吗?

$sql = "
  SELECT *
  FROM profile_img
  WHERE user_id = ?
  ORDER BY ? DESC LIMIT 1
  ";
  if ($stmt = $con->prepare($sql)) {
        $stmt->bind_param("ss", $user_id, `id`);
        $stmt->execute();
        if (!$stmt->errno) {
            // Handle error here
        }
        $stmt->bind_result($id, $user_id, $profilePic);

        $pics = array();
        while ($stmt->fetch()) {
            $pics[] = $profilePic;
        }

        echo '<img id="home-profile-pic" src=" '.$profilePic.'">';
  }
Run Code Online (Sandbox Code Playgroud)

Jag*_*ati 5

我认为你不能:

  • 在order by子句中使用占位符
  • 绑定列名:您只能绑定值 - 或变量,并将其值注入预准备语句中.

You can use number instead of field name in the 'order by' clause