dongluanjie8678 2014-04-09 22:34
浏览 53
已采纳

如何在phpmyadmin中运行PDO预处理语句(带有命名占位符)?

Gone are the days when I did something like this:
WHERE something = '".$query->real_escape_string($your_unsafe_value_here)."'

So I've made the move to prepared statements, but I'm a little confused on how to debug some of my queries now. Here's an example PDO prepared statement that uses named placeholders. I've taken this from the online php manual:

/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
                      FROM fruit
                      WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();

If I need to do a quick test on a query, how do I run something like this in phpmyadmin? (...or is there another mysql GUI that can easily handle these types of queries?)

  • 写回答

3条回答 默认 最新

  • dqt20140129 2014-04-09 23:58
    关注

    For my setup this is what I ended up doing. Hopefully this solution will help someone else, regardless of my setup details...

    1) I turned on query logging in my my.ini file (I'm using Windows / WAMP):

    [mysqld]
    
    log = "C:\wamp\logs\queries.log"
    

    2) I needed to tell PDO to emulate prepared statements (I'm using Laravel 4.1):

    // Before you run the query...
     $pdo = DB::connection()->getPdo();
     $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES , true);    
    // run query here...
    // and then die, so it's the last query ran
    die();
    

    3) Keep the queries.log file open in notepad++ and just reload the file when prompted by notepad++. Copy & paste the last query into phpmyadmin. Now I can debug the query!

    I'll probably later have to come up with a more automated approach, but this seems to work for now.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮