需要来自DB的信息

时间:2014-03-16 00:57:45

标签: php

我是PHP编码的新手,现在一位朋友帮了我很多,我很惭愧要求他修改我的代码。 我创建了一个链接,我得到category.php?nameID = ID,但是当我去category.php时,我想在这个页面上看到所有类别都是这样的: 1.类别 2.类别1 3.类别2 但所有可点击的,所以当我点击类别时,我去mysite.com/category.php?nameID=1,只看到类别1的内容, 例如,我想要这段代码:

echo "<b>" . $row['name'] . "</b> added on: " . $row['date'] . ", about this game <b>" 

也在category.php上,但包含所有类别

我现在有了这个代码:

<html>
<?php
 include 'config.php';

  if(isset($_GET['p'])){ 
   $p = mysql_real_escape_string($_GET['p']); 
   $mysql = (" SELECT * FROM category WHERE id ='".$p."'"); 
   $result = mysql_query($mysql); 
   $row = mysql_fetch_array($result);
   echo "<b>" . $row['name'] . "</b> added on: " . $row['date'] . ", about this game <b>" . $row['description'] . "</b>";

   }else{

   echo 'Nothing to find here';
  }
?>
</html>
非常感谢,我希望你能帮助我!

1 个答案:

答案 0 :(得分:0)

您遗漏了一些基本标签,例如<head><body>。我想你是在问这个:

<?php
include 'config.php';

// create a where clause
if (isset($_GET['p'])) $where="id=".(int)$_GET['p'];
else $where=1;

// select all or specific using the where clause
$mysql = "SELECT * FROM category WHERE ".$where;
$result = mysql_query($mysql);

// if there are rows returned
if (mysql_num_rows($result)){
    // iterate
    while($data=mysql_fetch_array($result)){
        echo "<li><a href=\"category.php?p=".$row['id']."\"><b>" . $row['name'] . "</b></a> added on: " . $row['date'] . ", about this game <b>" . $row['description'] . "</b>";
    }
}else{
    echo 'Nothing to find here';
}
?>

P.S。请不要使用mysql,它已被弃用;使用MySQLiPDO。在编写SELECT语句时,确保在SELECT之前没有空格 - 我发现如果字符串以S开头,SQL缓存只会缓存。