duanmo5724 2015-02-19 16:08
浏览 51
已采纳

我怎么能用LIKE做bindParam?

I'm trying create a LIKE using bindParam. When I try execute it does throws an exception.

How could I do it?

public function getLocalByNome($nomeLocal){
        $stm = $this->conexao->prepare("SELECT * FROM locais WHERE nome LIKE ?");
        $stm->bindParam(1, "%".$nomeLocal."%", PDO::PARAM_STR);
        $stm->execute();
        $lista = $stm->fetchAll();

        return $lista;
    }

Exception:

Fatal error: Cannot pass parameter 2 by reference in /opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php on line 94

  • 写回答

1条回答 默认 最新

  • dongwei1855 2015-02-19 16:13
    关注

    You can do one of two things.

    $tempString = "%".$nomeLocal."%";
    $stm->bindParam(1, $tempString, PDO::PARAM_STR);
    

    or

    $stm->bindParam(1, $tempString = "%".$nomeLocal."%", PDO::PARAM_STR);
    

    This error is thrown because you are binding a variable where as you can say

    $stm->bindParam(1, $tempString, PDO::PARAM_STR);
    $tempString = 'something else';
    

    and when you execute the statement it will use something else even though you set it after binding it to the pdo statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应