0

i want to Search my SQL table called users if they have a result in the structure called gangs then if that result is the one i'm looking for display all the found results in a list. here is the code i have so far witch is not working please help thanks

$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; ?>
Tprice88
  • 651
  • 2
  • 7
  • 18
  • 1
    You should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 02 '12 at 20:32
  • "is not working" is not very helpful. Not working how? – Hamish Aug 02 '12 at 20:33
  • its not listing any members witch do have the gang on the users tbale – Tprice88 Aug 02 '12 at 20:34

1 Answers1

2

It looks like you should just use a SELECT query with joins

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

if you are trying to build an array of result rows, this:

$members = $row['name'];

Should be:

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

You should also declare your $memberes variable before the loop like

$members = array();
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • what what be the rest of this code that will store the results into $gang_members – Tprice88 Aug 02 '12 at 20:40
  • @Tprice88 honestly I don't know what you are trying to do here. You have three different queries which seem to do related things and it seems you already know how to populate array from a MySQL result set, so I don't know what code you would want me to show. – Mike Brant Aug 02 '12 at 20:43
  • do i put this below? $members = $row['name']; – Tprice88 Aug 02 '12 at 20:43
  • See the edit in my answer. I didn't really look at the entire code that closely originally as it seems the queries were your issue. – Mike Brant Aug 02 '12 at 20:45
  • im not sure if this works or not i dont think it does, the gang table actually wont be needed at all for this, the results are stored in the users table and i just need to search users if they have the gangname im looking for then display a result of all the users found. – Tprice88 Aug 02 '12 at 20:53
  • Well if you want to use your original query then at least the array population should work, though it is not clear in your script where the $gang_name value comes from that you are using in your WHERE clause – Mike Brant Aug 02 '12 at 20:55
  • $gang_name is above on the code its found under the gang table and its the only reason for the gang sql code posted here and it works – Tprice88 Aug 02 '12 at 21:00
  • I see $Gang_name defined above but not $gang_name. Those are two different variables in PHP. – Mike Brant Aug 02 '12 at 21:02
  • wow thanks dident notice i had that mistake, using that and $members[] = $row['name']; has seemed to fix it thanks, btw was it true to switch to pdo or sqli i read that their both still very buggy – Tprice88 Aug 02 '12 at 23:26
  • @Tprice88 MySQLi and PDO are both suitable for production. – Mike Brant Aug 03 '12 at 14:15