0

I'm trying to make a query, to find out if a player is in a match, and if his status isn't quit (6). The query below isn't working though. What am I doing wrong?

Thanks

$query = mysql_query("SELECT * FROM matches WHERE 
        (player1Name = '$name' AND player1Status != 6) || 
        (player2Name = '$name' AND player2Status != 6) || 
        (player3Name = '$name' AND player3Status != 6) ||
        (player4Name = '$name' AND player4Status != 6) ||
        (player5Name = '$name' AND player5Status != 6) ||
        (player6Name = '$name' AND player6Status != 6)
        "); 
6
  • 1
    player1Status field is string or integer in database ? Commented Mar 17, 2012 at 9:12
  • just try to echo $query and check what it the result? I have reached you problem. just tell me immediately. Commented Mar 17, 2012 at 9:12
  • 3
    What does "Not working" mean exactly? Are you getting errors, or are you not getting any results? Commented Mar 17, 2012 at 9:12
  • can u provide table structure with sample data? Commented Mar 17, 2012 at 9:13
  • player1Status is an integer (tinyint(2)) Commented Mar 17, 2012 at 9:33

4 Answers 4

3

In SQL, || is the concatenation operator (like . in PHP). Use OR instead.

Sign up to request clarification or add additional context in comments.

2 Comments

It seems that's not entirely true for mySQL ||,OR is apparently logical operator.
2

Both OR and || are legit as it says here.You could try PDO format as recommended here:

The DB connection is like this (with your db details, of course):

$dsn = "mysql:host=127.0.0.1;dbname=reportslave1";
$username = "root";
$password = "";
try {
    $DBH = new PDO($dsn, $username, $password);
}
catch(PDOException $e) {
    echo $e->getMessage();
} 

The prep work like this:

$STH = $DBH->prepare("SELECT * FROM matches WHERE 
      (player1Name = :name AND player1Status != 6) OR
      (player2Name = :name AND player2Status != 6) OR
      (player3Name = :name AND player3Status != 6) OR
      (player4Name = :name AND player4Status != 6) OR
      (player5Name = :name AND player5Status != 6) OR
      (player6Name = :name AND player6Status != 6) 
    ");

$STH->bindParam(":name",$name);

The call is then made as follows.

try {
  $STH->execute();
  $STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
  echo $e->getMessage();
}

1 Comment

A pleasure, sir :) (Or ma'am - can't be sure!)
2

I would suggest to change the structure of your schema. If you ever find yourself creating columns like player1Name,player1Status, player2Name, player2Status, player3Name, there's almost certainly something wrong with your schema. You could try something like this

matches
-------
match_id (int)
...other match info columns

players
-------
player_id (int)
player_name (varchar)

matches_players
---------------
match_id
player_id
status

Your query could then easily become

SELECT *
FROM matches_players mp
    INNER JOIN players p ON mp.player_id=p.player_id
WHERE p.name='$name'
    AND mp.status != 6

10 Comments

I concur with the liquorvicar - there has to be a better way to organise the DB :)
sounds much better... I have about 60 rows in the "active_matches" structure... 8 rows for each players... I don't quite get how the above code works.. Is there any good documentation or tutorial where I can learn about how to create a good schema? THanks
@user1251004 Well, database design is a huge topic: en.wikipedia.org/wiki/Database_design I'm not sure I can recommend anything as I've learnt by experience, by osmosis.
If it was me, I would follow the liquor vicar's example and assign a unique match id and player id to the match and player record. I wouldn't have a matches_players table, though. When a player joins a game, I'd update fields in the match and player records. So... Match has: id, p1, p2, p3, p4, p5, p6. Player has: id, status, m_num, p_num (match number and player number). If player #179 joins match 32 as the 3rd player, you find player id 179 and set status to 1, m_num to 32 and p_num to 3. You also find match 32, and set p3 to 179. When the player quits, you unset everything/set status to 6.
@Nick en.wikipedia.org/wiki/Database_normalisation would be a good starting point for further reading. This isn't really "my" wisdom, it's fairly basic database design theory...
|
0

Got it try below code:

$query = mysql_query("SELECT * FROM matches WHERE 
        (player1Name = '".$name."' AND player1Status != 6) OR 
        (player2Name = '".$name."' AND player2Status != 6) OR 
        (player3Name = '".$name."' AND player3Status != 6) OR
        (player4Name = '".$name."' AND player4Status != 6) OR
        (player5Name = '".$name."' AND player5Status != 6) OR
        (player6Name = '".$name."' AND player6Status != 6)
        "); 

This will work.

4 Comments

I dont think so , it will make any difference.
That doesn't make any change...magic quotes should only be used in case of array variables like '".$row['variable']."' or '".$_SESSION['variable']."'
it makes sense guys. I think you don't know that $var will be print as same as it is within a single quote and that's why the php cann't recognise that is a variable or a string.
@GauravVashishtha That is only true if the string is enclosed in single quotes. The OP's query is enclosed in double quotes (inside which variables are evaluated), it merely contains single quotes. Example: codepad.org/8QsTfUPF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.