Bind result to variable in PHP

Description

The following code shows how to bind result to variable.

Example


//from w  w w  .j a v a  2  s. c  o m
<?php

    // Create a new server connection
    $mysqli = new mysqli('localhost', 'root', '', 'test');

    // Create query
    $query = 'SELECT id, name, birthday FROM employee';

    // Create a statement object
    $stmt = $mysqli->stmt_init();

    // Prepare the statement for execution
    $stmt->prepare($query);

    // Execute the statement
    $stmt->execute();

    // Bind the result parameters
    $stmt->bind_result($sku, $name, $price);

    // Cycle through the results and output the data
  
    while($stmt->fetch())
        printf("%s, %s, %s <br />", $sku, $name, $price);

    // Recuperate the statement resources
    $stmt->close();

    // Close the connection
    $mysqli->close();

?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented