【问题标题】:PHP-SQL Prepared statements with variable numeric table name [duplicate]具有可变数字表名的 PHP-SQL 准备语句 [重复]
【发布时间】:2017-12-12 15:10:35
【问题描述】:

我需要帮助来编写这段代码:

<?php

$a = $_GET["a"];

$stmt = mysqli_stmt_init($conn);

if (mysqli_stmt_prepare($stmt,"SELECT * FROM `?`")) {

   mysqli_stmt_bind_param($stmt, "i", $a);
   mysqli_stmt_execute($stmt);
   $res = mysqli_stmt_get_result($stmt);

   while($row = mysqli_fetch_array($res)){
      if($row["id"] == 0){
         $title = $row["title"];
         $mal = $row["mal"];
      }
   }

   mysqli_stmt_close($stmt);
}

?>

在我的数据库中,我有一些带有数字名称的表 (1,2,3...),我想通过 url 中的变量 $a 获取我想要的表。

【问题讨论】:

  • “并且没有找到任何在线搜索” - 通过在 Google 中输入“prepared statements 动态表名”找到重复项。

标签: php mysql mysqli prepared-statement tablename


【解决方案1】:

您不能使用准备好的语句绑定对象名称(在这种情况下为表名称),只能使用值。您将不得不求助于字符串操作(为简洁起见,省略了 SQL 清理):

if (mysqli_stmt_prepare($stmt,"SELECT * FROM `$a`")) { # Here!

   mysqli_stmt_execute($stmt);
   $res = mysqli_stmt_get_result($stmt);

【讨论】:

  • 省略了 SQL 清理,这个答案的值是无到负。
  • 如果我错了告诉我,这样我不会容易被注射吗?
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 2012-08-29
  • 2013-11-09
相关资源
最近更新 更多