Return multiple values from a function using mysqli_fetch_assoc [closed]



PHP Snippet 1:

function db_select($sql){  
    $rows = array();
    $result = db_query($sql);
        if(!$result){
            return false;
        }

        // This function could potentially return multiple rows...
        while($array = mysqli_fetch_assoc($result)){
            // This will return multiple rows
            $row[] = $array;
        }

        // This will return only one row
        // (but there may be more that you are missing)
    //  $row = mysqli_fetch_assoc($result)

    // Notice change here
    return $row;
}       

if (isset($_POST['submit'])){
     $rows = db_select("SELECT C_ID,C_Fname FROM Customer WHERE C_Email='$Email'")

     // If you have the one row option you echo like so
 //     echo $rows['C_ID'];
 //     echo $rows['C_Fname'];

     // For multiple row option, do a foreach or if you know the key you can access it directly
     foreach($rows as $arrays) {
             echo $arrays['C_ID'];
             echo $arrays['C_Fname'];
         }

}

PHP Snippet 2:

while($row = mysqli_fetch_assoc($result)){
    $rows[] = $row;
}
return $rows;

PHP Snippet 3:

function db_select($sql){  
        $rows = array();
        $result = db_query($sql);
        if(!$result){
            return false;
         }else{
            $row = mysqli_fetch_assoc($result);
            return $row;
         }
}  

if (isset($_POST['submit'])){
     $rows = db_select("SELECT C_ID,C_Fname FROM Customer WHERE C_Email='$Email'")
     echo $rows['C_ID'];
     echo $rows['C_Fname'];
}