gpt4 book ai didi

php - 链接到 PHP MYSQL 中的排序表列

转载 作者:行者123 更新时间:2023-11-29 02:35:54 24 4
gpt4 key购买 nike

<?php
$con = mysql_connect ("localhost", "user", "pass")
or die ('Error: ' . mysql_error());
mysql_select_db ("members");

if(isset($_GET['orderby'])){
$order = $_GET['orderby'];
$result = "SELECT * FROM persons ORDER BY ".mysql_real_escape_string($order)." DESC";
}
else{
$result = mysql_query("select * from persons");
}

$num_rows = mysql_num_rows($result);
$row_counter = 0;

echo "<table width=600 border=0 cellspacing=0>\n";
echo "<tr>\n
<th>&nbsp;</th>\n
<th>First Name</th>\n
<th>Last Name</th>\n
<th>Email Address</th>\n
<th>City</th>\n
<th>State</th>\n
<th><a href='index.php?orderby=submitDate'>Date</a></th>\n
</tr>";

while($row = mysql_fetch_array($result)){
if($row_counter % 2){
$row_color="bgcolor='#FFFFFF'";
}
else{
$row_color="bgcolor='#F3F6F8'";
}
echo "<tr ".$row_color.">";
echo "<td class='id'>" . $row['id'] . "</td>\n";
echo "<td>" . $row['firstName'] . "</td>\n";
echo "<td>" . $row['lastName'] . "</td>\n";
echo "<td>" . $row['email'] . "</td>\n";
echo "<td>" . $row['city'] . "</td>\n";
echo "<td>" . $row['state'] . "</td>\n";
echo "<td>" . $row['submitDate'] . "</td>\n";
echo "</tr>";
$row_counter++;
}
echo "</table>";

mysql_close($con);
?

>

我只是想不通为什么我的查询排序链接不起作用。有任何想法吗?预先致谢!

最佳答案

您的 if else block 是错误的。在一种情况下 $result 是一个查询,在另一种情况下它是一个结果集。检查下面的代码.. 已修复。

<?php
$con = mysql_connect ("localhost", "user", "pass")
or die ('Error: ' . mysql_error());
mysql_select_db ("members");

if(isset($_GET['orderby'])){
$order = $_GET['orderby'];
$sql = "SELECT * FROM persons ORDER BY ".mysql_real_escape_string($order)." DESC";
}
else{
$sql = "select * from persons";
}
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
$row_counter = 0;

echo "<table width=600 border=0 cellspacing=0>\n";
echo "<tr>\n
<th>&nbsp;</th>\n
<th>First Name</th>\n
<th>Last Name</th>\n
<th>Email Address</th>\n
<th>City</th>\n
<th>State</th>\n
<th><a href='index.php?orderby=submitDate'>Date</a></th>\n
</tr>";

while($row = mysql_fetch_array($result)){
if($row_counter % 2){
$row_color="bgcolor='#FFFFFF'";
}
else{
$row_color="bgcolor='#F3F6F8'";
}
echo "<tr ".$row_color.">";
echo "<td class='id'>" . $row['id'] . "</td>\n";
echo "<td>" . $row['firstName'] . "</td>\n";
echo "<td>" . $row['lastName'] . "</td>\n";
echo "<td>" . $row['email'] . "</td>\n";
echo "<td>" . $row['city'] . "</td>\n";
echo "<td>" . $row['state'] . "</td>\n";
echo "<td>" . $row['submitDate'] . "</td>\n";
echo "</tr>";
$row_counter++;
}
echo "</table>";

mysql_close($con);
?>

关于php - 链接到 PHP MYSQL 中的排序表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132020/

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