PHP中的bind_param错误:“变量数与参数数量不匹配”

时间:2015-09-15 01:21:35

标签: php mysql mysqli bindparam

我很清楚这个问题之前已被问过,我很抱歉再次提问,但其他人没有回答我的情况。我不知道这个bind_param有什么问题。这是我的PHP代码:

<?php
$db = new mysqli("localhost", "HIDDEN", "HIDDEN", "HIDDEN");
if ($db->connect_error) {
   die("Sorry, there was a problem connecting to our database.");
}

$username = stripslashes(htmlspecialchars($_GET['username']));

$result = $db->prepare("SELECT * FROM messages");
$result->bind_param("s", $username);
$result->execute();

$result = $result->get_result();
while ($r = $result->fetch_row()) {
   echo $r[1];
   echo "\\";
   echo $r[2];
   echo "\n";
}

这个bind_param有什么问题?很抱歉再次打扰并感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您应该在prepare语句中使用placeholders(问号,?),并且bind_param方法的参数数量应为1 + num ?个符号

第一个参数表示参数的数据类型,后跟bind参数值。

在您的示例中,您将2个参数传递给bind_param方法(第一个参数,s表示字符串和第二个参数,$username表示值。),但没有占位符{{1}在你的查询中。

答案 1 :(得分:1)

那是因为你试图在这里$result->bind_param("s", $username);绑定变量import os,re def changeName(filename): with open(filename, "r") as f: line = next(f) secondline = next(f) if secondline == "textfile" + str(re.search(r'\d+', filename).group()): #re.search() gets the first integer in the filename os.rename(filename, secondline + ".txt") for root, dirs, files in os.walk("Directory"): for file in files: file = os.path.join(root, file) changeName(file)

问题是您没有在查询中使用它。