0

hi guys I am doing a simple pagination and getting that error where I am going wrong ?

Warning: mysqli::query(): Couldn't fetch mysqli in D:\wamp\www\sitemiz\admin\edit.php on line 54

<?php                   
        // Start pagination
        $sql = "SELECT count(*) FROM users";
      // Line 54 Showing this line //  $result = $conn->query($sql); 
        $total_pages = ceil($show_page); 

        echo "<a href='edit.php?page=1'>".'|<'."</a> "; // Goto 1st page  

        for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='edit.php?page=".$i."'>".$i."</a> "; 
        }; 
        echo "<a href='edit.php?page=$total_pages'>".'>|'."</a> ";
?>

total codes added I am using oop php, 7.2 php version 5.. mysql at localhost wamp server. Something wrong with last part of the code not sure what thanks guys

//connection is right
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
//First part of the codes 
                $show_page=5;
                if (isset($_GET["page"])) { 
                $page  = $_GET["page"]; 
                } else { 
                $page=1; 
                }; 
                  $start_from = ($page-1) * $show_page; 
                    $sql = "SELECT * FROM users LIMIT $start_from, $show_page";
                    if($result = $conn->query($sql)){
                        if($result->num_rows > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>#</th>";
                                        echo "<th>Name</th>";
                                        echo "<th>Address</th>";
                                        echo "<th>Salary</th>";
                                        echo "<th>Action</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row = $result->fetch_assoc()){
                                    echo "<tr>";
                                        echo "<td>" . $row['id'] . "</td>";
                                        echo "<td>" . $row['name'] . "</td>";
                                        echo "<td>" . $row['address'] . "</td>";
                                        echo "<td>" . $row['salary'] . "</td>";
                                        echo "<td>";
                                            echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                            echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                            echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                        echo "</td>";
                                    echo "</tr>";
                                }
                                echo "</tbody>";                            
                            echo "</table>";
                            // Free result set
                            $result->free();
                        } else{
                            echo "<p class='lead'><em>No records were found.</em></p>";
                        }
                    } 
        // Start pagination
        $sql = "SELECT count(*) FROM users";  
        $result = $conn->query($sql);  // Line 54 Showing this line //
        $total_pages = ceil($show_page); 

        echo "<a href='users.php?page=1'>".'|<'."</a> "; // Goto 1st page  

        for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='users.php?page=".$i."'>".$i."</a> "; 
        }; 
        echo "<a href='users.php?page=$total_pages'>".'>|'."</a> ";
  • Are u sure that the connection to your database is really established? – wayneOS Apr 05 '18 at 15:11
  • Your error message lists `mysqli` as the cause of your problem, but your question only mentions it in commented out line. We need the full code establishing the connection to help you. Please review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Stefan Crain Apr 05 '18 at 15:11
  • Total codes added to main question @Stefan Crain –  Apr 05 '18 at 17:47
  • looks like I am having problem at localhost, just uploaded it to server and it works great, same server versions why having error at local host ? strange –  Apr 05 '18 at 18:04

0 Answers0