$sql = $conn -> prepare("SELECT username, email FROM users WHERE username=? OR email = ?");
$sql -> bind_param('ss',$uname,$email);
$sql -> execute();
$result = $sql -> get_result();
$row=$result -> fetch_array(MYSQLI_ASSOC);
if ($row['username'] == $uname) {
echo "Username not availabe try different";
}
elseif ($row['email']==$email) {
echo "email is already taken diiferent";
}
else {
$stmt = $conn -> prepare("INSERT INTO users (username, email, pass, created) VALUES (?, ?, ?, ?)");
$stmt -> bind_param('ssss', $username, $email, $pass, $created);
if ($stmt->execute()) {
echo "Registrated sucessfully. Login Now!";
}
else {
echo "Something went wrong. Please try again";
}
}
此代码未运行。不知道为什么 如果我要抓取一行,那么它可以工作,但是要抓多个行,则不能工作。
下面我想验证是否一个用户名和电子邮件是已经退出或没有使用预准备语句的分贝。
请帮助。
答案 0 :(得分:2)
最后,经过3-4个小时的锻炼,我借助以下堆栈溢出文章解决了这个问题:call-to-undefined-method-mysqli-stmtget-result
如果任何人有,因为本机驱动程序的同样的问题,参考这篇文章。 这是对我有用的解决方案,如下所示:首先定义此功能
function fetchAssocStatement($stmt)
{
if($stmt->num_rows>0)
{
$result = array();
$md = $stmt->result_metadata();
$params = array();
while($field = $md->fetch_field()) {
$params[] = &$result[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
if($stmt->fetch())
return $result;
}
return null;
}
您可以看到它创建了一个数组并使用行数据来获取它,因为它在内部使用了$stmt->fetch()
,因此可以像调用mysqli_result::fetch_assoc
一样调用它(只需确保$ stmt对象已打开并存储了结果)。现在请上面的函数: -
//mysqliConnection is your mysqli connection object
if($stmt = $mysqli_connection->prepare($query))
{
$stmt->execute();
$stmt->store_result();
while($assoc_array = fetchAssocStatement($stmt))
{
//do your magic
}
$stmt->close();
}
答案 1 :(得分:1)
好。我试过了: MYSQLi
var standard = standardRoom();
var quad = quadRoom();
var total = standard + quad;
$("#total").html(total)
PDO
$sql = $conn -> prepare("SELECT * FROM users WHERE username=? OR email = ?");
$sql -> bind_param('ss',$uname, $email);
$sql -> execute();
$result = $sql -> get_result();
$row=$result -> fetch_array(MYSQLI_ASSOC);
对我来说很好。您收到500错误,因此请检查您的prepare语句。我认为有一个错误导致500。