"No database selected" 在使用 SQL/PHP 实施 Zebra_pagination 时
"No database selected" when implementing Zebra_pagination with SQL/PHP
PHP 相对较新,并且是第一个 Stack 问题,因此对于我的任何错误提前致歉。
我正在尝试使用我的 SQL 数据库(使用最新的 MAMP)实现 Zebra Pagination,但出现 'No database selected' 错误。我认为问题在于我如何连接我与 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();
?>
我猜这里只是初学者犯了一些愚蠢的错误。 Any/all 帮助将不胜感激!
我一定会在本地使用 "mysql" 命令或 Sequel Pro(开源、捐赠软件)等图形工具来测试您的凭据。
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.
所以问题可能不在 Zebra 身上。虽然这可能是权限问题,但无论哪种方式,很可能是您的 PHP 和 MySQL 之间的配置错误,如此处所述:PHP says "No Database selected" even after using mysqli_select_db()
PHP 相对较新,并且是第一个 Stack 问题,因此对于我的任何错误提前致歉。
我正在尝试使用我的 SQL 数据库(使用最新的 MAMP)实现 Zebra Pagination,但出现 'No database selected' 错误。我认为问题在于我如何连接我与 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();
?>
我猜这里只是初学者犯了一些愚蠢的错误。 Any/all 帮助将不胜感激!
我一定会在本地使用 "mysql" 命令或 Sequel Pro(开源、捐赠软件)等图形工具来测试您的凭据。
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.
所以问题可能不在 Zebra 身上。虽然这可能是权限问题,但无论哪种方式,很可能是您的 PHP 和 MySQL 之间的配置错误,如此处所述:PHP says "No Database selected" even after using mysqli_select_db()