duanchazhou6779 2016-01-24 20:29
浏览 32

PDO准备了选择声明

I have been tasked to search a database using a PDO with prepared select statement i have been given which looks like this

SELECT * FROM ? WHERE ? = '?'

I have managed to get the PDO to conduct the search but it is totally incorrect, ive tried to use the examples I have seen on here but nothing seems to do the job here is how i temp fixed it to search for me

try {

//Create array of prepared sql commands to select * from db tables avoiding SQL Injection
$sql = $dbh->prepare("show tables");

$sql->execute();

if ( $sql->columnCount() > 0 )
{
  while ($row = $sql->fetch() )
  //$sqls[ $row[0] ] = "select * from " . $row[0] . ";";
    $sqls[ $row[0] ] = "select * from " . $row[0] . " where $fieldname = '?';";
    $sql = $dbh->prepare($sqls[$tablename]);
    $sql->execute()
}

and here is my attempt to use "?"

try {

//Create array of prepared sql commands to select * from db tables avoiding SQL Injection
$sql = $dbh->prepare("show tables");

$sql->execute();

if ( $sql->columnCount() > 0 )
{
  while ($row = $sql->fetch() )
  //$sqls[ $row[0] ] = "select * from " . $row[0] . ";";
    $sqls[ $row[0] ] = "select * from ? where '?' = '?';";
    $sql = $dbh->prepare($sqls[$tablename]);
    $sql->bindParam(1,$tablename);
    $sql->bindParam(2,$fieldname);
    $sql->bindParam(3,$celldata);
    $sql->execute()
}

this of course didn't work, i tried the 's' method to bind the parameter that didn't work either i know there's definitely something up with [ $row[0] ] I understand this goes to the first row of the table, but do not understand where it fits in with the goal of creating something like this

SELECT * FROM ? WHERE ? = '?'

any help or pointers in the right direction would be really appreciated, thank you so much

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?