SQL/MySQL/Procedure Function/PHP

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

Calling a stored procedure in PHP and deal with the result

   <source lang="sql">

<html> <head> <title>Employee listing</title> </head> <body>

Employee listing

<form method="post" >

Enter Department ID: <input type="text" name="dept_id" size="4"> <input type="submit" name="submit" value="submit"><p> </form> <?php $hostname = "localhost"; $username = "root"; $password = "secret"; $database = "prod"; if (IsSet ($_POST["submit"])) { $dbh = new mysqli($hostname, $username, $password, $database); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit (); } $dept_id = $_POST["dept_id"]; if ($result_set = $dbh->query("call employee_list( $dept_id )")) { print ("

". "");
         while ($row = $result_set->fetch_object()) {
printf("\n",
                     $row->employee_id, $row->surname, $row->firstname);
         }
    } else {
printf("

Error:%d (%s) %s\n", mysqli_errno($dbh), mysqli_sqlstate($dbh), mysqli_error($dbh)); } print ("</table> "); $dbh->close(); } ?> </body></html> </source>

Using PHP to call a stored procedure and output the result

   <source lang="sql">

Department listing

Employee_idSurnameFirstname
%s%s%s

<?php

  $hostname="localhost";
  $username="root";
  $password="secret";
  $database="sqltune";
  
  $p1="";
  $p2="";
   
  $dbh = new mysqli($hostname, $username, $password, $database);
  /* check connection */
  if (mysqli_connect_errno()) {
     printf("Connect failed: %s\n", mysqli_connect_error());
     exit();
  }
  if ($result_set = $dbh->query("call department_list()"))
  {
     printf("");
     while($row=$result_set->fetch_object())
     {
printf("\n",
                 $row->department_id, $row->department_name);
     }
  }
  else // Query failed - show error
  {
printf("

Error retrieving stored procedure result set:%d (%s) %s\n", mysqli_errno($dbh),mysqli_sqlstate($dbh),mysqli_error($dbh)); $dbh->close(); exit(); } /* free result set */ $result_set->close(); $dbh->close(); ?> </table> </body> </html> </source>

Department ID Department Name
%s%s