Logo  

CS479/579 - Web Programming II

Displaying ./code/Mysqli/view-prepared.php

<?php
  include "config.php";

  // The ? in the prepared query will be replaced with this value:
  $input_role = "Faculty";

  $query = "select name,role from people where role=?";
  $stmt = $myconn->prepare($query) or
    die ("Error creating prepared statement: ($query) " . $myconn->error);

  // Binds $input_role to the ? in the prepared statement:
  $stmt->bind_param("s", $input_role);
  $name = $role = null;
  // Binds $name and $role to the outputs of the prepared statement
  $stmt->bind_result($name, $role);

  // Executes the prepared statement.
  $stmt->execute();

  // The fetch() method is true until there is no more data:
  // $name and $role will be updated on each fetch().
  while ($stmt->fetch()) {
    printf("Name: %s (%s)\n", $name, $role);
  }

  // Change the input and re-execute the query:
  $input_role = "Student";
  $stmt->execute();
  while ($stmt->fetch()) {
    printf("Name: %s (%s)\n", $name, $role);
  }

?>