Data not being returned, but no error

Discussion in 'PHP' started by JA12, Apr 3, 2009.

  1. #1
    I've got a problem, I can't see why the following code doesn't return any data, or at least any data that I can use. The $result doesn't produce an error but there's no data, the key I'm using definitely exists.

    
    <?php
    $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
    //Select database
    mysql_select_db(DB_DATABASE, $conn) or die(mysql_error());
    ?>
    <body>
    <?php include "../scripts/header.php"; ?>
    <?php
    $sql = "SELECT u.* FROM " . DB_DATABASE . "." . $table . " u WHERE u.ID=" . $uid . "";
    $result = mysql_query($sql);
    if(!$result) {
    	die('MySQL Error: ' . mysql_error());
    exit;
    
    }
    $user = mysql_fetch_assoc($result);
    ?>
    <script language="javascript" type="text/javascript">
    <?php 
    echo 'alert("'. $user['username'] . '");';
    ?>
    </script>
    snip...
    
    Code (markup):
    The answer is probably so simple, but I just can't see the solution.
     
    JA12, Apr 3, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, try simplifying everything at first, to see if you get a result:
    $sql = mysql_query("SELECT * FROM $table WHERE u.ID = '$uid'");
    $result = mysql_numrows($sql);
    echo $result;

    This will show you if you get any results from the query
     
    PoPSiCLe, Apr 3, 2009 IP
  3. JA12

    JA12 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've just discovered that mysql_fetch_assoc is case sensitive

    so $user['username'] is not the same as $user['Username']...

    Idiot language!
     
    JA12, Apr 3, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Uhm, yes, PHP is Case Sensitive, yes...
     
    PoPSiCLe, Apr 3, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Be happy about it. Case-insensitivity is a major flaw which leads to countless bugs. This is programming we're talking about, not impressionist painting; precision is a virtue.
     
    SmallPotatoes, Apr 3, 2009 IP
  6. JA12

    JA12 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for that. :D
     
    JA12, Apr 4, 2009 IP