dqx76962 2018-09-15 21:38
浏览 72

使用PDO的execute(array())插入语句不起作用[复制]

I'm trying to INSERT a PDO Prepared statement and make it secure. I have one working example and the trouble comes with the EXECUTE() when I add a 2nd Variable into the array. MySQL v5.5.60

$Table = "devices";
$Name = "Garage Door";

Does not work

function allResults($Table, $Name) {
    $pdo = Database::connect();
    $sql = $pdo->prepare("SELECT * FROM :table WHERE Name = :name ORDER BY ID DESC");
    $sql->execute([':name' => $Name, ':table' => $Table]);
    $data = $sql->fetch(PDO::FETCH_ASSOC);
    return $data;
}

Does not work

    function allResults($Table, $Name) {
    $pdo = Database::connect();
    $sql = $pdo->prepare("SELECT * FROM :table WHERE Name = :name ORDER BY ID DESC");
    $sql->execute(array(':name' => $Name, ':table' => $Table));
    $data = $sql->fetch(PDO::FETCH_ASSOC);
    return $data;
}

Works!

    function allResults($Table, $Name) {
    $pdo = Database::connect();
    $sql = $pdo->prepare("SELECT * FROM $Table WHERE Name = :name ORDER BY ID DESC");
    $sql->execute(array(':name' => $Name));
    $data = $sql->fetch(PDO::FETCH_ASSOC);
    return $data;
}

Thanks

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 易康econgnition精度验证
    • ¥15 线程问题判断多次进入
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致