Link to home
Start Free TrialLog in
Avatar of Victor Kimura
Victor KimuraFlag for Canada

asked on

php mysqli - getting results with for loop

Hi,

I'm just wondering how I can retrieve data using mysqli using a for loop and foreach loop rather than using a while statement. I heard or read somewhere that while is a little slower.

I have this bit of code:
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT name, email FROM freecd WHERE email LIKE ? LIMIT 5")) {

    $stmt->bind_param("s", $code);
    $code = "vkim%";

    $stmt->execute();

    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);

    /* fetch values */
    while ($stmt->fetch()) {
        printf("%s %s\n", $col1, $col2);
    }

    /* close statement */
    $stmt->close();
} else {

      echo $mysqli->error;
}

Thanks,
vkimura
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The "while" is not slower.  Use it!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor Kimura

ASKER

Thank you.