Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用MySQLI处理SELECT查询结果时出现问题_Php_Mysqli_User Registration - Fatal编程技术网

Php 使用MySQLI处理SELECT查询结果时出现问题

Php 使用MySQLI处理SELECT查询结果时出现问题,php,mysqli,user-registration,Php,Mysqli,User Registration,我尝试使用的基本控制结构是使用用户名和电子邮件查询数据库,这两个都是唯一的密钥,如果其中一个在数据库中,请让用户知道它们已被使用,并选择其他内容。我遇到的问题是以可用的形式获取结果数据,然后我可以对照用户提供的数据进行检查 我从snippit中删除了准备好的插入语句,以及验证例程,因为它们都工作得很好 数据库连接snippit 注册脚本剪贴画 我很确定答案在于迭代和使用来自结果mysqli对象的meta_数据,但在我的头撞到墙上几天,像疯子一样翻阅mysqli php手册页面之后,我仍然不知道我

我尝试使用的基本控制结构是使用用户名和电子邮件查询数据库,这两个都是唯一的密钥,如果其中一个在数据库中,请让用户知道它们已被使用,并选择其他内容。我遇到的问题是以可用的形式获取结果数据,然后我可以对照用户提供的数据进行检查

我从snippit中删除了准备好的插入语句,以及验证例程,因为它们都工作得很好

数据库连接snippit

注册脚本剪贴画


我很确定答案在于迭代和使用来自结果mysqli对象的meta_数据,但在我的头撞到墙上几天,像疯子一样翻阅mysqli php手册页面之后,我仍然不知道我应该做什么。有人能给我指出正确的方向吗?

从注册脚本开始,您是否尝试过以下方法:

if($stmt = $dbc->prepare("SELECT `email`, `username` FROM `users` WHERE `email` = ? OR `username` = ?"))
{
    $stmt->bind_param("ss", $e, $u);
    $stmt->execute();
    $stmt->bind_result($email, $username);
    $rows = $stmt->num_rows;

    //Move Conditionals Up a Little
    if( $rows == 0 ) {    //If No Records are Found

        //Continue Registration
    }
    else if( $rows == 1 ) { //If One Record is Found

        $stmt->fetch();

        //Do Something With $email and $username from DB Here

    }
    else { //If More than One Record is Found

        while( $stmt->fetch() ) {  //Iterate Through Records

            //Do Something With $email and $username from DB Here
        }
    }
}

你知道,你使用异常的想法是非常错误的。。。原创。我试着读你的代码,但放弃了。对我可怜的小老鼠来说,滚动太多了。说到你的问题——只是你的不耐烦。在获取任何注册码之前,您必须练习简单的mysqli示例,熟悉基本操作。bind_结果如何?对不起,我知道我还是一个初学者,是的,是不耐烦让我问这个问题,但我觉得它可以帮助任何其他可能与我有相同问题的人。你通过结果集的逻辑是惊人的。谢谢,这有点帮助。如果我有代表,我会投这个票。这并不是一个完整的答案,但绝大多数情况下肯定不仅仅是朝着正确方向迈出的一步。谢谢。那样的话,我的答案在哪里?
//before this was validation routines, if anything was wrong the script generated something into $reg_errors which is an array.
if(empty($reg_errors))
{
//queries database if there are any matches for username or email from user input.
if($stmt = $dbc->prepare("SELECT `email`, `username` FROM `users` WHERE `email` = ? OR `username` = ?"))
{
    $stmt->bind_param("ss", $e, $u);
    $stmt->execute();
    $stmt->store_result();
    $rows = $stmt->num_rows;                   //gives the number of rows returned from SELECT query. 0 means no dupes, 1 means one record has BOTH email and username, 2 means two different records (one with email, one with username)

 ##THIS IS WHERE I'M RUNNING INTO TROUBLE GETTING THE DATA IN A USABLE FORM##

    $stmt->close();
} else {
    echo "<p>Can't talk to database right now. Try again later, please.</p>";
}

        if($rows==0)            //no dupes of username or email, so let's try and add them into the DB
        {
            //prepared statement for insertion into DB
            //also get's the count of affected rows. 1 means record inserted correctly. 

        //asks DB if a new row was created, and if so, thanks user for 
        //registration on the site & sends an email to their email.
        //if query doesnt work, an error is triggered
        if($count==1) {
            //constructs a thank you note and emails it to the user, using the email they supplied.
            exit(); 
            } else {
                echo "<p>Unable to process your registration at this time. Please try again later..</p>";
            }
    } else {                    // both username and email might be already used in DB, and error msgs are generated for array.
        if($rows==2) {          // this checks to make sure both entries are dupes        
            $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.';
            $reg_errors['username'] = 'This username has already been registered. Please try another.';
        } else {                //this checks to see which of the two (email or username) is already in DB if both arent dupes.

            if((__NEED SOMETHING HERE FROM DB QUERY___ == $_POST['email']) && (__NEED SOMETHING HERE FROM DB QUERY___ == $_POST['username'])) {   //both match entries in DB
                $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.';
                $reg_errors['username'] = 'This username has already been registered with this email address. If you have forgotten your password, use the link to the right to have your password sent to you.';
            } elseif(__NEED SOMETHING HERE FROM DB QUERY___==$_POST['email']) {        // email match
                    $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.';
            } elseif(__NEED SOMETHING HERE FROM DB QUERY___==$_POST['username']) {     // username match
                $reg_errors['username'] = 'This username has already been registered. Please try another one.';
            }
        }       // end of $rows==2 ELSE
    }           // end of $rows == 0 IF

    } else {           // end of empty reg_errors conditional
        //do something if the reg_error array isnt empty..
    }  
if($stmt = $dbc->prepare("SELECT `email`, `username` FROM `users` WHERE `email` = ? OR `username` = ?"))
{
    $stmt->bind_param("ss", $e, $u);
    $stmt->execute();
    $stmt->bind_result($email, $username);
    $rows = $stmt->num_rows;

    //Move Conditionals Up a Little
    if( $rows == 0 ) {    //If No Records are Found

        //Continue Registration
    }
    else if( $rows == 1 ) { //If One Record is Found

        $stmt->fetch();

        //Do Something With $email and $username from DB Here

    }
    else { //If More than One Record is Found

        while( $stmt->fetch() ) {  //Iterate Through Records

            //Do Something With $email and $username from DB Here
        }
    }
}