2
$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";
    }
}

此代码未运行。我不知道为什么。如果我正在获取单行,那么它正在工作,但对于多行它不工作。

在这里,我想使用准备好的语句验证数据库中是否已经存在用户名和电子邮件。

4

2 回答 2

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();
}
于 2019-02-02T13:01:43.377 回答
1

好的。我试过: MYSQLi

$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);

PDO

$sql = $conn -> prepare("SELECT username, email FROM users WHERE username= :uname OR email = :email");
        $sql -> bindParam(':uname', $uname);
        $sql -> bindParam(':email', $email);
        $sql -> execute();

        $row=$sql -> fetchAll();
        print_r($row);

对我来说很好。您收到 500 错误,因此请检查您的准备语句。我认为有一个错误导致500。

于 2019-02-02T09:32:23.103 回答