gpt4 book ai didi

PHP 在数据库中的分页表中排序

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:49 24 4
gpt4 key购买 nike

我得到它来订购,但现在分页不起作用。现在没有任何内容打印到我的错误日志中。

用户 Red Acid 帮助我解决了缺少的空格并使用字符串变量而不是 PHP_SELF。

一旦我按下下一条记录,我得到:

Not Found

The requested URL /surf/$index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我的代码:

<?php
$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbName = "";
$tbl_name="";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';

mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");


$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);

if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){

?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$scriptname?page=$page\">Next 10 Records</a>";
}

else if( $page == 0 )
{
echo "<a href=\"$$scriptname?page=$page\">Next 10 Records</a>";
}

else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$$scriptname?page=$last\">Last 10 Records</a>";
}
mysql_close();
?><?php
$dbHost = "localhost";
$dbUser = "c1226125_ms";
$dbPass = "wR(@ucrb5oG.";
$dbName = "c1226125_timer";
$tbl_name="ck_playerrank";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';

mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");


$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);

if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){

?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$scriptname?page=$page\">Next 10 Records</a>";
}

else if( $page == 0 )
{
echo "<a href=\"$$scriptname?page=$page\">Next 10 Records</a>";
}

else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$$scriptname?page=$last\">Last 10 Records</a>";
}
mysql_close();
?>

最佳答案

问题是 ORDER BY 和 DESC 之间没有空格:

$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"].$_GET["desc"];

应该是:

$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"]." ".$_GET["desc"];

使用字符串变量代替 PHP_SELF:

$scriptname = 'myscript.php'; // OR http://domain.com/myscript.php
echo "<a href=\"$scriptname?page=$last\">Last 10 Records</a>";

关于PHP 在数据库中的分页表中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32676313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com