How do I get specific data from a database?

Discussion in 'PHP' started by Imozeb, Mar 31, 2010.

  1. #1
    I want to get 'name' value from my database. How do I do this?

    This is what I've been trying.

    PHP code:
    $rdata = mysql_query("SELECT NAME FROM User_Database WHERE PASS= '$inputpass'", $databasename) or die(mysql_error());
    	$row = mysql_fetch_assoc($rdata);
    	$name = $row['NAME'];
    echo($name);
    Code (markup):
    Why won't it work, it doesn't give an error but $name = null

    Thanks.

    ~imozeb
     
    Imozeb, Mar 31, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    lets change your code to
    
    $inputpass = addslashes($inputpass);
    $sql = "SELECT `NAME` FROM `User_Database` WHERE `PASS`= '{$inputpass}'";
    $rdata = mysql_query($sql, $databasename) or die(mysql_error().'<br />'.$sql);
    $row = mysql_fetch_assoc($rdata);
    $name = $row['NAME'];
    echo($name);
    var_dump($rdata);
    Code (markup):
    Are you sure those column names are meant to be uppercase?

    I echo out the sql if there is an error because it can then be pasted right into phpMyAdmin and tested. Visually you might also see the problem.

    `NAME` tells mysql not to consider name to be a reserved word but to look for a column called name.

    {$inputpass} with {} around it tells PHP to parse the variable. Its supposedly faster and certainly safer.
     
    sarahk, Mar 31, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. It worked. And thanks for explaining what you did instead of just giving me the code. I'll remember that curly bracket thing.
     
    Imozeb, Apr 1, 2010 IP
    sarahk likes this.