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."'"); 

注2php5.5で警告が生成される場合でも、mysql_*関数の使用は非推奨になっているため、pdoを学習したい場合は、ここで使用するか、代わりに優れたチュートリアルを使用してください。E_DEPRECATEDPDOMySQLi

于 2012-12-12T11:16:33.527 に答える
1

Maybe you are looking for something like this?:

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

In order to do ascending and descending sorting, you need to add another parameter to your link. Should it be asc or desc by default is your call, whatever makes more sense. In this case I made 0 for ascending and 1 for descending.

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

The next thing you want to do is to add logic for sort handling:

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

And the last thing to do is the change of asc to desc and vice versa when you click the link second time. If you put the code I provided (logic for sort handling) and the code for the $order handling above your navigation table (to make the variables accessible within the table), you can add a condition into the link:

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

And you MySQL query will look something like this:

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