这个问题之前已经得到了回答,但格式不同。
我正在尝试创建一个抓取数据库行的对象方法。我一直得到标题错误。奇怪的是,如果我直接在父类方法中调用函数的相同代码,它就可以工作。以下是两个例子。
示例1:有效 从单独的PHP页面调用的函数如: $ email = trim($ _ POST ['email']); $ password = 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();
}
}
现在,如果我在同一个方法中调用该函数但是从扩展类调用(父类是扩展DatabaseOptions的User)。这些函数是这样编写的:
public static function authenticate($username="", $password="") {
$dbObject = static::get_row('email', $username);
if (isset($dbObject)) {
return $dbObject;
}
}
DatabaseObject中的函数:
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();
}
}
答案 0 :(得分:1)
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();
。