如何在函数中传递变量:table和id

时间:2016-05-10 10:35:11

标签: php

数据库

    <?php 

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';

$link = new mysqli($host, $user, $password, $database);

/* check connection */
if ($link->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

?>

查看列表功能

    function viewList($table, $id){
  global $link;
  $table = $table;
  $id = $id;

  $query = 'SELECT * FROM '. $table .' ORDER BY '. $id .' DESC';
  $statement = mysqli_query($link, $query) or die ('A problem with database');
  // $result = $statement->get_result();
  // $statement->store_result();
  // $check = $result->num_rows;
  $check = mysqli_num_rows($statement);

 // $result = mysqli_query($mysqli,$query) or die('a problem with database');
 // $check = mysqli_num_rows($result);
   if($check > 0){
        echo '<div class="table-responsive">';
        echo '<table class="table table-condensed table-striped table-bordered table-hover no-margin">';
        echo '<thead>';
        echo '<tr>';
        echo '<th style="width:40%">Name</th>';
        echo '<th style="width:20%" class="hidden-phone">Address</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';

        while ($row = mysqli_fetch_array($statement)):
          echo '<tr>';
                echo '<td>'. $row['employee_name'] . '</td>';
                echo '<td>'. $row['employee_address'] . '</td>';
                echo '</tr>';
       endwhile;

       echo '</tbody>';
       echo '</table>';
       echo '</div>';
       $link->close();
    }
   else{
    echo '<div class="alert alert-warning">No records found</div>';
   }
}

在视图记录页面

     <?php
           include 'database.php';
           include "functions/crud.php";
           viewList("employees", "employee_id");           
   ?>

问题是结果未显示在页面上 - 空白。没有出现错误消息。 &#34;未找到任何记录&#34;打印出来。在数据库中,有3条记录。

是不是只能在viewList()函数中传递变量?想要为具有不同表和id名称的某些页面添加功能的简易性。

0 个答案:

没有答案