0

I've got a Sql table and I need to search through the members column in order to see if any of the values are the same as one gained previously (eg: 76561198119598543) and if it is then I want to get the value stored under name of the same row...

The link below is an example of what the table looks like and how the members column is set out. http://nbdcommunity.com/staffimages/example.png

I'm not too much of a PhP person and have tried pretty much everything I could find.. $gangsql = 'SELECT members, name, owner FROM 'gangs' WHERE owner LIKE "$playerid%"'; (playerid being the value I'm wanting to compare to)

Pred
  • 17
  • 2
  • That is a terrible way to achieve what you're wanting to do from a database structure point of view. Ideally you should look into pivot tables. – Jono20201 Sep 10 '16 at 19:08
  • 1
    It's all I've got to work with. I can't edit the table and how it stores the contents as it works with a game server which I don't have access to configure. – Pred Sep 10 '16 at 19:11
  • If you are seeking an exact match, ' LIKE' is a bit slow/fuzzy, why not `=` ? If the database has an index on that column (it looks promising, if it is an id ;-) ... then you should be good to go with the modified query `SELECT members, name, owner FROM 'gangs' WHERE owner = '$playerid''` and if `$playerid` could ever come from some "region outside of your trus boundaries" ... than use prepared queries and do not interpolate some string into your query ... (hint) it might contain quite creative SQL amendments like e.g. `whatever' OR 1 = 1; DROP TABLE owner; --` – Dilettant Sep 10 '16 at 23:49

3 Answers3

0

It looks like values in column members are not numbers, but strings. Assuming it's the case, you can try :

SELECT name FROM gangs WHERE members LIKE '%76561198119598543%'

This will return the value of "name" for all rows where the "members" column contains the number you check.

HarvestR
  • 1
  • 2
0

Here is what i understand about your problem:
It's like a search box. If the data you want to look is matched, you display it?

Example:

PHP FILE

    <?php

    //Connection here

    if isset($_POST["member"]){
    $get_data = $_POST["member"];
    //Display matched Data
    $sql = mysql_query("SELECT * FROM table_name WHERE member = '$get_data' ");
    while($row = mysql_fetch_array($sql)){
    //Declare variables
    $this_name = $row["name"];

    //Display Variable
    echo 
    'The name you are looking for is this person; on this case it\'s a number <u>'.$this_name.'</u>';

    }
    //END WHILE LOOP
    }else{
    //No match

     echo '
     <script>
     alert("Data has no match. Please try again");
     window.location= "php_page_name.php";
     </script>
         ';
     }

     ?>

HTML HERE

<form method="post" action="">
Search Member:
<input type="text" name="member">
<input type="submit" value="Filter">
</form>

Hope this helps. Good luck!

Adrian
  • 29
  • 9
0
$lastValue = "76561198119598543";

$c = new mysqli($server, $user, $password) or die("cant connect");

if($res = $c->query("SELECT name FROM table WHERE members='$lastValue'")){
  if($res->num_rows > 0){
     $resAssoc = $res->fetch_assoc();
     $name = $resAssoc["name"];
  }
} else {
   die($c->error);
}

$c->close();
Benjamin
  • 1,067
  • 20
  • 30