0

我有一个投资组合,我想按照 ASC 排序顺序列出,根据列表顶部的几个字段进行选择。不排序打印输出是完美的,但是在激活排序语句(第 35 行)时,我收到以下错误消息:

警告:mysql_fetch_array():提供的参数不是第 47 行 /home/domain/public_html/db_name/portfolio.php 中的有效 MySQL 结果资源

有人可以帮忙告诉我如何处理这段代码吗?非常感谢您的任何建议:

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Portfolio</title>
<link rel="stylesheet" type="text/css" href="../scripts/css/formate.css" />
</head>
<body>

<?php 
 // Connects to Database 
 mysql_connect("localhost","user","passw") or die(mysql_error()); 
 mysql_select_db("db_name") or die(mysql_error()); 
 ?>

<table>
    <tr>
        <th><a href="?orderBy=ref_id">Order by Villa name   |</a></th>
        <th><a href="?orderBy=bedrooms">Order by no. of Beds   |</a></th>
        <th><a href="?orderBy=max_occupants">Order by Sleeps   </a></th>
    </tr>
</table>

<?php
// sort table as selected
$orderBy = array('ref_id', 'bedrooms', 'max_occupants');
$order = 'ref_id';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
    $order = $_GET['orderBy'];
}

// Read table ---> and sort it:

// $data = mysql_query("SELECT * FROM res_properties"); 

 $data = mysql_query("SELECT * FROM db_row ORDER BY '.$order"); 

 Print "<table border cellpadding=3>"; 
 Print "<br><h3>Portfolio</h3 ><p> <br>";
 echo 'Listings as per: -       ';
 print date('r');
 print "\\n";
 Print "<br><p> <br>";

 Print "<table border cellpadding=3 >"; 
 Print "<tr align='center'><th width=130>Villa Name</th><th width=40>Beds</th><th width=40>Baths</th><th width=60>Sleeps</th><th width=200 >Property Website </th><th width=50 >Prop.ID</th></tr>"; 

   while($info = mysql_fetch_array( $data )) 

{ 

 Print "<tr align='center'><font face='arial' size='2' color='000000'>"; 
 Print "<td>".$info['ref_id'] . "</td> "; 
 Print "<td>".$info['bedrooms'] . " </td>"; 
 Print "<td>".$info['bathrooms'] . " </td>"; 
 Print "<td>".$info['max_occupants'] . " </td>"; 
 Print "<td><a href=\"http://www.domain.com/properties/index.php/property/" . $info['slug'] . ".html\">Open website here</a></td>";
 Print "<td>".$info['id'] . "</td></tr> "; 

 } 
 Print "</table>"; 
 ?> 

</body>
</html>
4

3 回答 3

3

我认为这只是您构建查询的方式。

 $data = mysql_query("SELECT * FROM db_row ORDER BY '.$order"); 

 $data = mysql_query("SELECT * FROM db_row ORDER BY " . $order); 

如果这不起作用,则在执行该语句后添加mysql_error并查看错误是什么。

于 2012-12-12T11:15:30.313 回答
2

尝试

$data = mysql_query("SELECT * FROM db_row ORDER BY '$order'"); 

或者

 $data = mysql_query("SELECT * FROM db_row ORDER BY " . $order."'"); 

注意 2不推荐mysql_*使用函数,即使它会在php5.5中生成警告,所以如果你想在这里学习 pdo ,请使用或代替,这是很好的教程E_DEPRECATEDPDOMySQLi

于 2012-12-12T11:16:33.527 回答
1

也许您正在寻找这样的东西?:

$data = mysql_query('SELECT * FROM db_row ORDER BY ' . $order . ' ASC'); 

为了进行升序和降序排序,您需要在链接中添加另一个参数。如果它是ascdesc默认情况下是你的电话,无论什么更有意义。在这种情况下,我为上升设置了 0,为下降设置了 1。

<a href="?orderBy=ref_id&sort=0">

您要做的下一件事是为排序处理添加逻辑:

$sortBy = array('asc', 'desc');
$sort = 0;
if (isset($_GET['sort']) && in_array($_GET['sort'], array_keys($sortBy))) {
    $sort = $_GET['sort'];
}

最后要做的是在您第二次单击链接时更改ascto desc,反之亦然。如果您将我提供的代码(排序处理逻辑)和处理代码$order放在导航表上方(以使变量在表中可访问),则可以在链接中添加条件:

<a href="?orderBy=ref_id&sort=<?php echo ($order == 'ref_id' ? !$sort : 0); ?>">

您的 MySQL 查询将如下所示:

$data = mysql_query('SELECT * FROM db_row ORDER BY ' . $order . ' ' . $sortBy[$sort]); 
于 2012-12-12T11:17:33.510 回答