mysqli_stmt::bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配(一个函数有效,另一个无效)
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement (one function works other does not)
这个问题以前有人回答过,但格式不一样。
我正在尝试创建一个 object 方法来获取数据库行。我不断收到标题错误。奇怪的是,如果我直接在 parent class 方法中调用函数的相同代码,它就可以工作。下面是两个例子。
示例 1:有效
从单独的 PHP 页面调用的函数,例如:
$email = trim($_POST['email']);
$密码 = trim($_POST['password']);
$found_user = User::authenticate($email, $password);
这是正在运行的函数。 (如果我正在制作一个可重用的函数来测试我的代码是否错误,我会设置该函数)
public static function authenticate($username="", $password="") {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$field = 'email';
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
$stmt = $mysqli->stmt_init();
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $mysqli->error;
return $error;
}
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $stmt->error;
return $error;
} else {
$stmt->prepare($sql_query);
$valueType = gettype($username);
if ($valueType == 'string') {
$stmt->bind_param('s', $username);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $username);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $username);
} else {
$stmt->bind_param('b', $username);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
}
}
现在,如果我用相同的方法调用函数,但从扩展的 class(parent class 是扩展 DatabaseOptions 的用户)。函数是这样写的:
public static function authenticate($username="", $password="") {
$dbObject = static::get_row('email', $username);
if (isset($dbObject)) {
return $dbObject;
}
}
数据库对象中的函数:
public function get_row($field, $value) {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
$stmt = $mysqli->stmt_init();
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $stmt->error;
return $error;
} else {
$stmt->prepare($sql_query);
$valueType = gettype($value);
if ($valueType == 'string') {
$stmt->bind_param('s', $value);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $value);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $value);
} else {
$stmt->bind_param('b', $value);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
}
}
public function get_row($field, $value) {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
if ($stmt = $mysqli->prepare($sql_query)) {
$valueType = gettype($value);
if ($valueType == 'string') {
$stmt->bind_param('s', $value);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $value);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $value);
} else {
$stmt->bind_param('b', $value);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
} else {
$error = "Errormessage: " . $stmt->error;
return $error;
}
}
原始代码的问题在于我使用的是 bind_result($row),其中我从数据库中提取的行有多个需要绑定的值。如$result = $stmt->get_result
然后返回一个fetch_object();
.
这个问题以前有人回答过,但格式不一样。
我正在尝试创建一个 object 方法来获取数据库行。我不断收到标题错误。奇怪的是,如果我直接在 parent class 方法中调用函数的相同代码,它就可以工作。下面是两个例子。
示例 1:有效 从单独的 PHP 页面调用的函数,例如: $email = trim($_POST['email']); $密码 = trim($_POST['password']);
$found_user = User::authenticate($email, $password);
这是正在运行的函数。 (如果我正在制作一个可重用的函数来测试我的代码是否错误,我会设置该函数)
public static function authenticate($username="", $password="") {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$field = 'email';
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
$stmt = $mysqli->stmt_init();
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $mysqli->error;
return $error;
}
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $stmt->error;
return $error;
} else {
$stmt->prepare($sql_query);
$valueType = gettype($username);
if ($valueType == 'string') {
$stmt->bind_param('s', $username);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $username);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $username);
} else {
$stmt->bind_param('b', $username);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
}
}
现在,如果我用相同的方法调用函数,但从扩展的 class(parent class 是扩展 DatabaseOptions 的用户)。函数是这样写的:
public static function authenticate($username="", $password="") {
$dbObject = static::get_row('email', $username);
if (isset($dbObject)) {
return $dbObject;
}
}
数据库对象中的函数:
public function get_row($field, $value) {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
$stmt = $mysqli->stmt_init();
if (!$stmt->prepare($sql_query)) {
$error = "Errormessage: " . $stmt->error;
return $error;
} else {
$stmt->prepare($sql_query);
$valueType = gettype($value);
if ($valueType == 'string') {
$stmt->bind_param('s', $value);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $value);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $value);
} else {
$stmt->bind_param('b', $value);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
}
}
public function get_row($field, $value) {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT * ";
$sql_query .= "FROM `".static::$table_name."`";
$sql_query .= "WHERE {$field}=? ";
$sql_query .= "LIMIT 1";
if ($stmt = $mysqli->prepare($sql_query)) {
$valueType = gettype($value);
if ($valueType == 'string') {
$stmt->bind_param('s', $value);
} else if ($valueType == 'integer') {
$stmt->bind_param('i', $value);
} else if ($valueType == 'double') {
$stmt->bind_param('d', $value);
} else {
$stmt->bind_param('b', $value);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_object();
} else {
$error = "Errormessage: " . $stmt->error;
return $error;
}
}
原始代码的问题在于我使用的是 bind_result($row),其中我从数据库中提取的行有多个需要绑定的值。如$result = $stmt->get_result
然后返回一个fetch_object();
.