PHP 調用 MySQL 存儲過程并獲得返回值

phpde 9年前發布 | 1K 次閱讀 PHP

<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();

} ?></pre>

 本文由用戶 phpde 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!