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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?