Query not working for some reason, any ideas?

Discussion in 'PHP' started by twistedspikes, Dec 11, 2008.

  1. #1
    This is the code i'm trying to get working

    $user_id = $_SESSION['id'];
    				
    				$query = " SELECT * FROM users WHERE id = $user_id ";
    				$result = mysql_query($query) or die('Error, query failed');
                    
    				while($row = mysql_fetch_array($result))
    				{
    					echo '<p>Welcome ' . $row['name'] . '.</p>';
    				}
    PHP:
    Any idea why it's not?

    All that comes up when it's run is "Welcome ."

    I have tried putting echo $user_id; after it gets the id from the session, that gets it correctly. So basicly the error is somewhere in the query but I really have no idea where or why, i'm using variables like this in other queries on the site and they work :rolleyes:

    Anyway, any help would be great!

    Thanks,
    TS
     
    twistedspikes, Dec 11, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try this possibly:

    $query = " SELECT * FROM users WHERE id = '$user_id'";
     
    jestep, Dec 11, 2008 IP
    twistedspikes likes this.
  3. brownskinman

    brownskinman Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try

    
    session_start();
    $user_id = $_SESSION['id'];
    // be sure to clean / check variables
    $query = "SELECT * FROM `users` WHERE `id` = '".$user_id."' LIMIT 1";
    $result = mysql_query($query) or die('Error, query failed. '.mysql_error());
    
    $RS = array();
    while($row = mysql_fetch_assoc($result)) {
    	$RS[] = $row;
    }
    echo '<pre>';
    print_r($RS);
    echo '</pre>';
    
    PHP:
    to see what's in there..
     
    brownskinman, Dec 11, 2008 IP
    twistedspikes likes this.
  4. twistedspikes

    twistedspikes Notable Member

    Messages:
    5,694
    Likes Received:
    293
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Got it working :)

    Thanks guys :D much appreciated!
     
    twistedspikes, Dec 11, 2008 IP