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 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题