Php 在哪里放置mysqli fetch的while循环?

Php 在哪里放置mysqli fetch的while循环?,php,mysqli,Php,Mysqli,我使用mysqli和php能够从数据库中选择一列,也能够将数据插入数据库。现在在研究mysqli时,我发现在检查行数是否等于0之前,我需要包含一个while($stmt->fetch()){ 现在,因为我必须处理代码块,一个用于SELECT,另一个用于INSERT,我想知道while fetch循环是假设环绕整个代码还是假设分别环绕SELECT代码块和INSERT代码块 更新: $query = "SELECT TeacherAlias FROM Teacher WHERE Teache

我使用mysqli和php能够从数据库中选择一列,也能够将数据插入数据库。现在在研究mysqli时,我发现在检查行数是否等于0之前,我需要包含一个
while($stmt->fetch()){

现在,因为我必须处理代码块,一个用于SELECT,另一个用于INSERT,我想知道while fetch循环是假设环绕整个代码还是假设分别环绕SELECT代码块和INSERT代码块

更新:

    $query = "SELECT TeacherAlias FROM Teacher WHERE TeacherAlias = ?";
       // prepare query
       $stmt=$mysqli->prepare($query);
       // You only need to call bind_param once
       $stmt->bind_param("s",$getid);
       // execute query
       $stmt->execute();
       // get result and assign variables (prefix with db)
       $stmt->bind_result($dbTeacherAlias);
       //get number of rows
       $stmt->store_result();
       $numrows = $stmt->num_rows();
       $results = $stmt->fetch_all();


    foreach ($results as $row) {
       if ($numrows == 0){    

           // don't use $mysqli->prepare here
       $query = "SELECT TeacherUsername FROM Teacher WHERE TeacherUsername = ?";
       // prepare query
       $stmt=$mysqli->prepare($query);
       // You only need to call bind_param once
       $stmt->bind_param("s",$getuser);
       // execute query
       $stmt->execute(); 
       // get result and assign variables (prefix with db)
       $stmt->bind_result($dbTeacherUsername);
       //get number of rows
       $stmt->store_result();
       $numrows = $stmt->num_rows();
       $results = $stmt->fetch_all();
    }

foreach ($results as $row) {  
       if ($numrows == 0){
                                               // don't use $mysqli->prepare here
       $query = "SELECT TeacherEmail FROM Teacher WHERE TeacherEmail = ?";
       // prepare query
       $stmt=$mysqli->prepare($query);
       // You only need to call bind_param once
       $stmt->bind_param("s",$getemail);
       // execute query
       $stmt->execute(); 
       // get result and assign variables (prefix with db)
       $stmt->bind_result($dbTeacherEmail);
       //get number of rows
       $stmt->store_result();
       $numrows = $stmt->num_rows();
       $results = $stmt->fetch_all();
}

}
}
}
Try,返回一个关联数组:

$results = $stmt->fetch_all();

foreach ($results as $row) {
    // do something...
}

因此,我应该将
$results=$stmt->fetch_all();
放在
$numrows=$stmt->num_rows;
的下面,然后将foreach循环放在
if($numrows==0)的上面{
语句并关闭底部的循环?想想看:如果
$numrows==0)
您将没有任何东西可以循环。本质上,
count($results)
应该与
$numrows
相同。验证这一点并不是一件坏事,因为这是测试一切是否正常工作的一种方法。好的,那么代码所说的是遍历所有获取的行,然后使用if语句,如果找不到行,则执行某些操作。foreach是否遍历整个代码,或者每个循环是否需要多个。一个用于SELECT语句查询,一个用于INSERT语句查询?@user1394925是的,这将是我的方法。要么这样,要么将循环放在
else
语句中。你能看看我更新的代码吗?我是否将每个循环的for语句放在了正确的位置?你真的只是复制了一个循环吗回答?
// if no results found it will return either empty array or null (not sure, check it)
$results = $stmt->fetch_all();

// if $results is an empty array it will not enter the loop
foreach($results as $row) {
    if ($numrows == 0){   // this is never reached if results no results were found