0

database

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

?>

View List function

    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>';
   }
}

in view record page

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

The problem is that the result is not displayed on page -- blank. No error message appears. "No records found" is printed. in database, there are 3 records.

Is it not possible to just pass variables in the viewList() function? Wanted the ease of add function to some pages with different tables and id name.

joe
  • 1,115
  • 5
  • 21
  • 50
  • 2
    run the query "SELECT * FROM employees ORDER BY employee_id DESC " Directly in database console/phpmyadmin – NIRANJAN S. May 10 '16 at 10:40
  • Run the query directly on your MySQL console. Also, turn on error reporting. Add these lines `error_reporting(E_ALL); ini_set('display_errors', 1);` at the very top of your PHP scripts and see if it yields any error or not. – Rajdeep Paul May 10 '16 at 10:48
  • @niranjans,@rajdeeppaul - thanks and will do. Another question - not sure if it is wise to call function with variables? I mean not sure if its safe to do that. Thanks again – joe May 10 '16 at 12:35
  • @RajdeepPaul: before i made query into a function, I tested and this query worked but after moving it into function, it didn't work anymore. weird – joe May 11 '16 at 01:14
  • @joe Did you call the function, like this: `viewList(table_name, your_id);`? – Rajdeep Paul May 11 '16 at 01:20
  • @RajdeepPaul - yeah found why. there are no data in database. sorry. one more is it okay to call function with vars in view page? – joe May 11 '16 at 01:30
  • @joe, Yes, you can do that. – Rajdeep Paul May 11 '16 at 01:32
  • @RajdeepPaul but is it safe? – joe May 11 '16 at 01:52
  • @joe From what? SQL Injection? – Rajdeep Paul May 11 '16 at 01:55
  • @RajdeepPaul - yes from sql injection – joe May 11 '16 at 02:01
  • @joe If you're using `mysqli`, learn about [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Also, [this is how you can prevent SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Rajdeep Paul May 11 '16 at 02:02
  • @RajdeepPaul - already changed code to use prepare query. but i ran into problem. hmm. thanks for links tho – joe May 11 '16 at 02:09

0 Answers0