选择查询的顺序在准备好的语句中不起作用 [英] Select query's order not working in prepared statement

查看:42
本文介绍了选择查询的顺序在准备好的语句中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个准备好的选择查询,看起来查询没有选择 DESC 或者我的 bind_param 结构错误.我正在尝试获取要显示的 user_id 图像的最后一个 id.显示用户的图像,但这是他们拥有的第一个 id 图像.我尝试做 ASC 并且结果是一样的.

I am created a prepared select query and it appears the query is not picking up the DESC or I have the bind_param structured wrong. I am trying to get the last id of the user_id's image to display. The user's image displays, but it is the first id image they have. I tried doing ASC and it was the same thing.

我这样做对吗?

$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.'">';
  }

推荐答案

我不认为你可以:

  • 在 order by 子句中使用占位符
  • 绑定列名:您只能绑定值——或变量,以及将它们的值注入到准备好的语句中.

您可以在order by"子句中使用数字代替字段名称

这篇关于选择查询的顺序在准备好的语句中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆