Skip to content
Advertisement

“No database selected” when implementing Zebra_pagination with SQL/PHP

Relatively new to PHP and first Stack question, so apologies in advance for any mistakes on my part.

I’m trying to implement Zebra Pagination with my SQL database (using latest MAMP) and getting an error of ‘No database selected’. I think the issue lies in how I’m hooking up my connection with Zebra, but not having a ton of luck debugging. The code:

    <?php

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// how many records should be displayed on a page?
$records_per_page = 10;

// include the pagination class
require 'Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

$MySQL = "SELECT SQL_CALC_FOUND_ROWS Id 
          FROM  table
          LIMIT
        ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '
          ";
$result = $conn->query($MySQL);

// if query could not be executed
if (!($result = @mysql_query($MySQL))) {

    // stop execution and display error message
    die(mysql_error());

}

// fetch the total number of records in the table
$rows = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() AS rows'));

// pass the total number of records to the pagination class
$pagination->records($rows['rows']);

// records per page
$pagination->records_per_page($records_per_page);

?>

<table class="Id" border="1">

    <tr><th>Id</th></tr>

    <?php $index = 0?>

    <?php while ($row = mysql_fetch_assoc($result)):?>

    <tr<?php echo $index++ % 2 ? ' class="even"' : ''?>>
        <td><?php echo $row['Id']?></td>
    </tr>

    <?php endwhile?>

</table>

<?php 
// render the pagination links
$pagination->render();

$conn->close(); 
?>

I’m guessing there’s just some dumb beginner’s mistake in here. Any/all help would be much appreciated!

Advertisement

Answer

I would be sure to test your credentials using the “mysql” command locally or a graphical tool such as Sequel Pro (open source, donationware).

To cite Zebra’s source code:

Please note that this is a generic pagination script, meaning that it does not display any records and it does not have any dependencies on database connections or SQL queries, making it very flexible! It is up to the developer to fetch the actual data and display it based on the information returned by this pagination script. The advantage is that it can be used to paginate over records coming from any source like arrays or databases.

So the issue likely does not lie with Zebra. While it’s possible it is a permissions issue, either way it’s very likely a mis-configuration between your PHP and your MySQL, as referenced here: PHP says “No Database selected” even after using mysqli_select_db()

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement