MySQL Tutorial/Procedure Function/PHP — различия между версиями

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

Текущая версия на 09:53, 26 мая 2010

Calling a stored procedure in PHP

$dbh = new mysqli($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
if ($dbh->query("call error_test_proc(1)"))  /*execute stored procedure*/
{
   printf("Stored procedure execution succeeded");
}
else // Stored procedure failed - show error
{
   printf("<P>Stored procedure error: MySQL error %d (SQLSTATE %s)\n %s\n", $dbh->errno,$dbh->sqlstate,$dbh->error);      
}


Calling a stored procedure in PHP and deal with the result

<html>
<head>
<title>Employee listing</title>
</head>
<body>
<h1>Employee listing</h1>
<form method="post" >
<P>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 ("<table border="1" width="30%"> <tr> ".
               "<td>Employee_id</td><td>Surname</td><td>Firstname</td></tr>");
          while ($row = $result_set->fetch_object()) {
               printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", 
                      $row->employee_id, $row->surname, $row->firstname);
          }
     } else {
          printf("<P>Error:%d (%s) %s\n", mysqli_errno($dbh), 
                 mysqli_sqlstate($dbh), mysqli_error($dbh));
     }
     print ("</table> ");
     $dbh->close();
}
?>
</body></html>


Using PHP to call a stored procedure and output the result

<h1>Department listing</h1>
<table border="1" width="90%">
   <tr> <td><b>Department ID</b></td>
        <td><b>Department Name</b></td>
<?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("<tr><td>%s</td><td>%s</td></tr>\n",
                  $row->department_id, $row->department_name);
      }
   }
   else // Query failed - show error
   {
      printf("<P>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>