使用 SQL/PHP 实现Zebra_pagination时的"No database selected"



对PHP和第一个Stack问题相对较新,所以提前为我的任何错误道歉。

我正在尝试使用我的 SQL 数据库(使用最新的 MAMP)实施 Zebra 分页,但收到"未选择数据库"的错误。我认为问题在于我如何连接我与 Zebra 的连接,但没有很多运气调试。代码:

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

我猜这里只是一些愚蠢的初学者的错误。任何/所有的帮助将不胜感激!

我一定会在本地使用"mysql"命令或图形工具(如 Sequel Pro(开源、捐赠软件))测试您的凭据。

引用 Zebra 的源代码:

请注意,这是一个通用的分页脚本,这意味着它不显示任何记录,并且对数据库连接或SQL查询没有任何依赖关系,因此非常灵活!由开发人员获取实际数据并根据此分页脚本返回的信息显示它。优点是它可用于对来自任何源(如数组或数据库)的记录进行分页。

因此,问题可能不在于斑马技术。虽然这可能是权限问题,但无论哪种方式,都很可能是PHP和MySQL之间的配置错误,如下所述:即使使用mysqli_select_db()后,PHP也会说"未选择数据库"

最新更新