PHP/MySQL Database/mysqli prepare

Материал из Web эксперт
Перейти к: навигация, поиск

Result Binding in Prepared Statements

 
<?php
    $mysqli = mysqli_connect("hostname", "user", "pass", "database");
    if(mysqli_connect_errno()) {
        die("Could not connect: ".mysqli_connect_error());
    }
    $query = "SELECT first, last, phone FROM contacts WHERE first LIKE "John%"";
    $stmt = mysqli_prepare($mysqli, $query);
    mysqli_execute($stmt);
    mysqli_stmt_bind_result($stmt, $first, $last, $phone);
    while(($res = mysqli_stmt_fetch($stmt))) {
        echo "First: $first<BR/>\n";
        echo "Last: $last<BR/>\n";
        echo "Phone: $phone<BR/>\n";
    }
    if($res === false) {
        die("An error occurred fetching: ".mysqli_error($mysqli));
    }
    mysqli_stmt_close($stmt);
    mysqli_close($mysqli);
?>