我如何用PHP搜索SQL表

时间:2012-08-02 20:29:49

标签: php sql game-engine

我想搜索我称为用户的SQL表,如果他们在名为gangs的结构中有结果,那么如果该结果是我要查找的那个在列表中显示所有找到的结果。这是我到目前为止巫婆没有工作的代码请帮助谢谢

$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);
$profile = htmlspecialchars($row->profile);
$gang = htmlspecialchars($row->gang);

<?php 
$sql = "SELECT * FROM Gangs WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql)  or die(mysql_error());
$row = mysql_fetch_object($query);
$Gang_name = htmlspecialchars($row->name);
$Gang_owner = htmlspecialchars($row->owner);
$Gang_money = htmlspecialchars($row->money);
$Gang_exp = htmlspecialchars($row->exp);
$Gang_level = htmlspecialchars($row->level);
$Gang_profile = htmlspecialchars($row->profile);
?>

<?php
$result = mysql_query("SELECT * FROM users WHERE gang = '".$gang_name."'");
if ($result) {
      while($row = mysql_fetch_assoc($result)) {
           $members = $row['name'];
      }
}
?>
<?php echo $members; ?>

1 个答案:

答案 0 :(得分:2)

看起来你应该只使用带有连接的SELECT查询

"SELECT * FROM users as u JOIN gangs as g on u.gang = g.name WHERE g.name = '".mysql_real_escape_string($_GET['name'])."'";

如果您正在尝试构建结果行数组,请执行以下操作:

$members = $row['name'];

应该是:

$members[] = $row['name'];

您还应该在循环之前声明$ memberes变量,如

$members = array();