MySQLI Prepared Statement: num_rows & fetch_assoc

前端 未结 6 1714
我寻月下人不归
我寻月下人不归 2020-12-16 21:22

Below is some poorly written and heavily misunderstood PHP code with no error checking. To be honest, I\'m struggling a little getting my head around the maze of PHP->MySQLi

6条回答
  •  半阙折子戏
    2020-12-16 21:55

    True, the Databasefunctions are a bit weird. You'll get there.

    The code looks a bit iffy, but heres how it works:

    A connection is build, a statement prepared, a parameter bound and it's executed, all well.

    $result = $stmt->execute(); //execute() tries to fetch a result set. Returns true on succes, false on failure.
    $stmt->store_result(); //store_result() "binds" the last given answer to the statement-object for... reasons. Now we can use it!
    
    if ($stmt->num_rows >= "1") { //Uses the stored result and counts the rows.
    
      while($data = $result->fetch_assoc()){ 
            //And here, the answer-object is turned into an array-(object)
            // which can be worked with nicely.
            //It loops trough all entries in the array.
      }
    
    }else{
    
       echo "0 records found";
    }
    

提交回复
热议问题