【问题标题】:Unable to concatenate sql in pdo statement [duplicate]无法在 pdo 语句中连接 sql [重复]
【发布时间】:2014-01-31 19:54:41
【问题描述】:

我目前有一个 Get 变量

$name = $_GET['user'];

我正在尝试将它添加到我的 sql 语句中,如下所示:

$sql = "SELECT * FROM uc_users WHERE user_name = ". $name;

然后运行

$result = $pdo -> query($sql);

我得到一个无效的列名。但这没有意义,因为如果我像这样手动提出请求

$sql = "SELECT * FROM uc_users WHERE user_name = 'jeff'";

我得到了列数据,只是当我将它作为获取变量输入时没有。我究竟做错了什么。我对 pdo 比较陌生。

更新: 现在我有以下内容:

$name = $_GET['user'];

$sql = "SELECT * FROM uc_users WHERE user_name = :name";
    //run the query and save the data to the $bio variable
    $result = $pdo -> query($sql);
    $result->bindParam( ":name", $name, PDO::PARAM_STR );
    $result->execute();

但我得到了

> SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
> error in your SQL syntax; check the manual that corresponds to your
> MySQL server version for the right syntax to use near ':name' at line
> 1

【问题讨论】:

标签: php mysql sql pdo


【解决方案1】:

为了使您的查询与没有变量的查询一样工作,您需要在变量周围加上引号,因此将您的查询更改为:

$sql = "SELECT * FROM uc_users WHERE user_name = '$name'";

但是,这很容易受到 SQL 注入的影响,因此您真正想要的是使用占位符,如下所示:

$sql = "SELECT * FROM uc_users WHERE user_name = :name";

然后按你的方式准备:

$result = $pdo->prepare( $sql );

接下来,绑定参数:

$result->bindParam( ":name", $name, PDO::PARAM_STR );

最后,执行它:

$result->execute();

【讨论】:

  • 从头开始,现在它说“SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取正确的语法在第 1 行的 ':name' 附近使用"
  • 当使用:name 占位符代替实际变量时,不要用单引号括起来。
  • 我没有用单引号括起来它正在工作,但它开始吐出那个错误
  • 所以你是说它正在工作,然后没有做任何改变它就停止工作了?
  • 是的,它刚刚停止。我使用了您建议的代码,使用 :name 作为占位符。我刚刚更新了我的代码
【解决方案2】:

我发现这在防止 SQL 注入的同时最符合我的口味:

编辑:正如@YourCommonSense 所指出的,您应该按照这些guidelines 使用安全连接

// $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$sql = 'SELECT * FROM uc_users WHERE user_name = ?';
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

// perhaps you'll need these as well
$count = $result->num_rows;
$row = $result->fetch_assoc();

/* you can also use it for multiple rows results like this
while ($row = $result->fetch_assoc()) {
    // code here...
} */

顺便说一句,如果您有更多参数,例如

$sql = 'SELECT * FROM table WHERE id_user = ? AND date = ? AND location = ?'

第一个? 是整数,第二个? 和第三个? 是字符串/日期/...您可以将它们绑定

$stmt->bind_param('iss', $id_user, $date, $location);
/*
 * i - corresponding variable has type integer
 * d - corresponding variable has type double
 * s - corresponding variable has type string
 * b - corresponding variable is a blob and will be sent in packets
 */

来源:php.net

编辑:

小心!您不能在 bind_param 中连接 $variables

相反,您在之前连接:

$full_name = $family_name . ' ' . $given_name;
$stmt->bind_param('s', $full_name);

【讨论】:

  • 这是一个正确的答案本身,但它有点不合时宜:问题是关于 PDO 而你的答案是关于 mysqli
  • 另外,仅供参考,连接只有一行是not enough
  • @YourCommonSense 感谢您的意见。我什至从没想过有什么区别。只是想摆脱 SQL 注入攻击的可能性。编辑了您的发帖链接。
【解决方案3】:

试试这个。你没有把单引号放在变量上。

$sql = "SELECT * FROM uc_users WHERE user_name = '". $name."'";

Note: 尝试使用 Binding 方法。这不是获取数据的有效方法。

【讨论】:

  • 语法错误,第 24 行 /home/content/46/11122346/html/skippl/cake/php/retrieve_bio.php 中的意外 T_CONSTANT_ENCAPSED_STRING
  • 您错过了查询的某些部分。您也可以尝试像尼克提到的"SELECT * FROM uc_users WHERE user_name = '$name'" 这样的查询。
  • 我收到“SQLSTATE[42000]:语法错误或访问冲突:1064”
  • 发布您的完整查询并field 输入数据库。
【解决方案4】:
$sql = "SELECT * FROM  'uc_users'  WHERE user_name = '". $name."' ";

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 2018-12-28
    • 2011-12-12
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    相关资源
    最近更新 更多