mysql error.query was empty

Discussion in 'PHP' started by ZheniaOdessa, Sep 10, 2012.

  1. #1
    Hello Guys, i need your help. I can't understand what is wrong with the code. i am doing left join
    When i query in mysql it works fine, i can see that left join works and the image_count row is in the album table.
    when i do it on the server,trying to make an album it says :Query was empty; what's wrong here?

    function get_albums() 
    {
        $albums = array();
        
        $albums_query = mysql_query("SELECT albums.album_id, albums.timestamp, albums.name, LEFT(albums.description, 50) as description, COUNT(images.image_id) as image_count 
    FROM albums 
    LEFT JOIN images 
    ON albums.album_id = images.album_id 
    WHERE albums.user_id =".$_SESSION['user_id']." 
    GROUP BY albums.album_id
        ");
        $res = mysql_query($albums_query) or die(mysql_error()); 
        
        while($albums_row = mysql_fetch_assoc($res)){
            $albums = array (
        
                    'id' => $albums_row['album_id'],
                    'timestamp'=> $albums_row['timestamp'],
                    'name' => $albums_row['name'],
                    'description' => $albums_row['description'],
                    'count' => $albums_row['image_count']
                    
                    
                    
       );
            
        }
                         
        return $albums;
        
    }
    
    PHP:
    another file

    print_r(get_albums:())
     
    Last edited: Sep 10, 2012
    ZheniaOdessa, Sep 10, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd start by changing the way you set up your query and your fail statement to

    $albums_query = "SELECT albums.album_id, albums.timestamp, albums.name, LEFT(albums.description, 50) as description, COUNT(images.image_id) as image_count
    FROM albums 
    LEFT JOIN images ON albums.album_id = images.album_id 
    WHERE albums.user_id = '{$_SESSION['user_id']}' 
    GROUP BY albums.album_id";
    
    $res = mysql_query($albums_query) or die(mysql_error().'<br>'.$albums_query);
    PHP:
    I think you'll find it magically comes right.

    If not, copy the query that gets echo'd out and put it into phpMyAdmin/SqlYog or whatever you use to manage your database.
     
    sarahk, Sep 10, 2012 IP
  3. ZheniaOdessa

    ZheniaOdessa Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a lot. it works fine now. i do really appreciate your help.

    Have a good day

    Zhenia
     
    ZheniaOdessa, Sep 11, 2012 IP