查看单击超链接时的详细MySQL结果

时间:2016-03-09 12:01:03

标签: php mysql

我试图实现以下目标。在主页上,MySQL查询返回表" parcid"(但结果有限)的结果。下一步是,我想超链接一个字段(最好是" ID"),使用相同的ID打开一个新页面,但返回更详细的结果。下面的代码有效,但显然我错过了某些地方,因为它返回的是所有结果,而不仅仅是点击ID的结果。任何帮助将不胜感激。

主页:" http://localhost/"

Pdetail url:" http://localhost/pdetail/"

  

查询" Home"页

<?php

// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query =  "SELECT * FROM parcid";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table cellpadding=1 >";
        echo "<tr>";
        echo "<td>"."ID"."</td>"; 
        echo "<td>"."City"."</td>";          
        echo "<td>"."Destination City"."</td>";
        echo "<td>"."Weight"."</td>";
        echo "<td>"."Length"."</td>";
        echo "<td>"."Width"."</td>";
        echo "<td>"."Height"."</td>";
        echo "<td>"."Type"."</td>";
        echo "<td>"."Courier Option"."</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($result)) {
        echo "<tr>";
        echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>';
        echo "<td>" . $row[6]."</td>";      
        echo "<td>" . $row[12]."</td>";
        echo "<td>" . $row[14]."</td>";
        echo "<td>" . $row[15]."</td>";
        echo "<td>" . $row[16]."</td>";
        echo "<td>" . $row[17]."</td>";
        echo "<td>" . $row[18]."</td>";
        echo "<td>" . $row[19]."</td>";
        echo "<td>" . $row[20]."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>
  

查询&#34; pdetailed&#34;页。

<?php

// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "";
$db = "wordpress";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

$ID = $_GET['ID'];

// create query
$query =   "SELECT * FROM parcid WHERE ID LIKE '%$ID%'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table cellpadding=1 >";
        echo "<tr>";
        echo "<td>"."ID"."</td>";
        echo "<td>"."CID"."</td>";
        echo "<td>"."From"."</td>";
        echo "<td>"."Street"."</td>";
        echo "<td>"."Suburb"."</td>";
        echo "<td>"."Post Code"."</td>";
        echo "<td>"."City"."</td>";
        echo "<td>"."Province"."</td>";
        echo "<td>"."Receiver"."</td>";
        echo "<td>"."Destination Street"."</td>";
        echo "<td>"."Destination Suburb"."</td>";
        echo "<td>"."Destination Postcode"."</td>";
        echo "<td>"."Destination City"."</td>";
        echo "<td>"."Destination Province"."</td>";
        echo "<td>"."Weight"."</td>";
        echo "<td>"."Length"."</td>";
        echo "<td>"."Width"."</td>";
        echo "<td>"."Height"."</td>";
        echo "<td>"."Type"."</td>";
        echo "<td>"."Courier Option"."</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($result)) {
        echo "<tr>";
        echo "<td>" . $row[0]."</td>";
        echo "<td>" . $row[1]."</td>";
        echo "<td>" . $row[2]."</td>";
        echo "<td>" . $row[3]."</td>";
        echo "<td>" . $row[4]."</td>";
        echo "<td>" . $row[5]."</td>";
        echo "<td>" . $row[6]."</td>";
        echo "<td>" . $row[7]."</td>";
        echo "<td>" . $row[8]."</td>";
        echo "<td>" . $row[9]."</td>";
        echo "<td>" . $row[10]."</td>";
        echo "<td>" . $row[11]."</td>";
        echo "<td>" . $row[12]."</td>";
        echo "<td>" . $row[13]."</td>";
        echo "<td>" . $row[14]."</td>";
        echo "<td>" . $row[15]."</td>";
        echo "<td>" . $row[16]."</td>";
        echo "<td>" . $row[17]."</td>";
        echo "<td>" . $row[18]."</td>";
        echo "<td>" . $row[19]."</td>";
        echo "<td>" . $row[20]."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

1 个答案:

答案 0 :(得分:1)

如果您只想要由$ ID标识的单行,则查询为

$query = "SELECT * FROM parcid WHERE ID = '$ID'";

此外,如果您知道只应从查询中收到一个结果行,则无需在while循环中运行mysql_fetch_row($result)

  

请不要使用the mysql_ database extension,它   已被弃用(在PHP7中永远消失)   特别是如果您只是学习PHP,请花时间学习PDOmysqli_数据库扩展,   and here is some help to decide which to use

除非你使用框架,否则这个陈述可能是错误的

echo '<td><a href="http://localhost/pdetail/'.$row[0].'">'.$row[0].'</a></td>';

也许它应该是

echo '<td><a href="http://localhost/pdetail?ID='.$row[0].'">'.$row[0].'</a></td>';