Need some help with php/sql

Discussion in 'PHP' started by micromark, Oct 14, 2007.

  1. #1
    Hi,

    I am using this code to display some info from 2 mysql tables.




    <?php
    $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image'
    AS image FROM 'giftlog'
    LEFT JOIN 'gifts'
    ON 'giftlog'.'giftid' = 'gifts'.'giftid'
    WHERE 'giftlog'.'recipientid' = " . $userid);
    echo $row['$name'];
    echo $row['$image'];
    ?>
    PHP:
    Problem is i get nothing !! No error just nothing !!

    So my question is, is the code wrong ? OR the sql tables ?



    Any1 got any help.
     
    micromark, Oct 14, 2007 IP
  2. Dakuipje

    Dakuipje Peon

    Messages:
    931
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There probably is an error:

    tried adding this to the end of your query ? :

     or die(mysql_error());
    PHP:
     
    Dakuipje, Oct 14, 2007 IP
  3. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You're missing the part where you get a row from the result set.

    $row = mysql_fetch_array($result);

    It should go after your first line.

    Typically you'd do this in a while loop if your query can return multiple rows.
     
    jnestor, Oct 14, 2007 IP
  4. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Still not working, any1 else got any idea's
     
    micromark, Oct 15, 2007 IP
  5. jonimontana

    jonimontana Well-Known Member

    Messages:
    262
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #5
    while ($row = mysql_fetch_array($query))
    {
    echo $row['$name'];
    echo $row['$image'];
    }
    Code (markup):
     
    jonimontana, Oct 15, 2007 IP
  6. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sorry tried that still not working.
     
    micromark, Oct 15, 2007 IP
  7. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Did you add the bit to output the mysql error if there is one?

    Best guess is that it is working but your query is returning nothing. Make sure your $userid is set correctly and run the query manually in PhpMyAdmin or the like to make sure the data in the table is correct.
     
    jnestor, Oct 15, 2007 IP
  8. micromark

    micromark Peon

    Messages:
    466
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No i didnt, how do i set the $userid correctly ?
     
    micromark, Oct 15, 2007 IP
  9. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #9
    when you run mysql_connect($databasename, $username, $password)

    make sure that $username=proper name either from the config, or in your file
     
    Lordy, Oct 15, 2007 IP
  10. Invent

    Invent Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    <?php
    $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image'
    AS image FROM 'giftlog'
    LEFT JOIN 'gifts'
    ON 'giftlog'.'giftid' = 'gifts'.'giftid'
    WHERE 'giftlog'.'recipientid' = " . $userid);
    
    $row = mysql_fetch_assoc( $result );
    
    echo $row['name']; // You don't use $ as it's not a variable.
    echo $row['image'];
    
    ?>
    
    PHP:
    That should work :)
     
    Invent, Oct 15, 2007 IP
  11. Maxim.Cerny

    Maxim.Cerny Active Member

    Messages:
    156
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #11
    Try this:

    $sql = 'SELECT ........';
    $res = mysql_query($sql);
    echo $sql; // for debuging
    echo mysql_error(); // for debuging
    while ($arr = mysql_fetch_array($res)) {
    echo $arr['id']; // or whatever
    }
    
    PHP:
     
    Maxim.Cerny, Oct 15, 2007 IP
  12. Invent

    Invent Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    
    <?php
    
    $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image'
    AS image FROM 'giftlog'
    LEFT JOIN 'gifts'
    ON 'giftlog'.'giftid' = 'gifts'.'giftid'
    WHERE 'giftlog'.'recipientid' = " . $userid) or die("MySQL Error with the query, error message: ".mysql_error());
    
    $row = mysql_fetch_assoc( $result ) or die("MySQL Error with the 'fetch_assoc', error message: ".mysql_error());
    
    echo $row['name']; // You don't use $ as it's not a variable.
    echo $row['image'];
    
    ?>
    
    PHP:
    Little revision on my code for better debugging.

    If you're only grabbing one row, then use the above code, otherwise use:

    
    <?php
    
    $result = mysql_query("SELECT 'giftlog'.'name' AS name, 'giftlog'.'image'
    AS image FROM 'giftlog'
    LEFT JOIN 'gifts'
    ON 'giftlog'.'giftid' = 'gifts'.'giftid'
    WHERE 'giftlog'.'recipientid' = " . $userid) or die("MySQL Error with the query, error message: ".mysql_error());
    
    while($row = mysql_fetch_assoc( $result ))
    {
    
    echo $row['name']; // You don't use $ as it's not a variable.
    echo $row['image'];
    
    }
    
    ?>
    
    PHP:
    Also, I've used fetch_assoc() for faster queries, if the returned data isn't in an associative array, then switch fetch_assoc to fetch_array.
     
    Invent, Oct 15, 2007 IP