Am i using php to select from mysql database correctly?

Discussion in 'PHP' started by Anveto, Oct 1, 2011.

  1. #1
    Is this correct, or what am i doing wrong?

    <?php
    $req = "SELECT *,' . $table . '.id as id_res  from $table, $table1 where ' . $table . '.player_id = ' . $table1 . '.id AND ' . $table . '.tour_id = ' . $q . '";
    ?>
    PHP:
     
    Anveto, Oct 1, 2011 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    You have variables all over the place. How about you attempt to execute the query, and see if it returns any errors.
     
    Alex Roxon, Oct 1, 2011 IP
  3. you-me

    you-me Peon

    Messages:
    67
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use this one

    <?php
    $req = "SELECT * ". $table .".id as id_res  from ".$table.", ".$table1." where '". $table ."'.player_id = '". $table1 ."'.id AND '". $table ."'.tour_id = '". $q;
    ?>
    PHP:
     
    you-me, Oct 2, 2011 IP
  4. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #4
    <?php
    $req = "SELECT *" . $table .".id as id_res from ".$table.", $table1 where " . $table . ".player_id = " . $table1 . ".id AND " . $table . ".tour_id = " . $q;
    ?>

    try above query.


    in php two string can be concatenated using ". "no coma is required.


    Sheetal
     
    SheetalCreation, Oct 2, 2011 IP
  5. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #5

    the query is correct but you may want to use table name aliases for better reading of the query..

    <?php
    $req = "SELECT *,a.id as id_res  from $table AS a, $table1 AS b where a.player_id = b.id AND a.tour_id = ' . $q . '";
    ?>
    PHP:
     
    JohnnySchultz, Oct 3, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    ' and " aren't the same in PHP.

    Assuming $table = "mytable", "SELECT *$table.id" will give you "SELECT *mytable.id" (which is bad SQL syntax, but has nothing to do with your confusing the 2 quotation methods). 'SELECT *$table.id' will give you 'SELECT *$table.id' - $table won't be parsed, it'll be the literal string of a dollar sign in front of the letters 'table'.

    If you confuse them you won't get the results you want.
     
    Rukbat, Oct 7, 2011 IP