php variable variables

Discussion in 'Web Design and Development' started by seventeen, Oct 21, 2009.

  1. seventeen macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #1
    I am writing this comment class:

    Code:
    class Comment {
    	
    	public $id;
    	public $post_id;
    	public $name;
    	public $email;
    	public $website;
    	public $body;
    	public $date;
    	public $ip_address;
    	public $status;
    	
    	function __construct($id) {
    		
    		global $db;
    
    		$resc = $db->query("SELECT * FROM blog_comments WHERE id='$id' LIMIT 1");
    		
    		while($row = $db->fetch_assoc($resc)) {
    			while ($comment = current($row)) {
    	    		$key = key($row);
    				$this->$key = $comment{$key};
    				next($row);
    			}
    		}
    	}
    }
    Here is what the query inside the constructor will return when run in the database:

    [​IMG]

    But when I run it, this is what print_r(new Comment(1)); spits out:

    Code:
    Comment Object
    (
        [id] => 1
        [post_id] => 1
        [name] => J
        [email] => j
        [website] => h
        [body] => b
        [date] => 1
        [ip_address] => :
        [status] => 1
    )
    Any ideas why I'm only getting the first character of each field?

    Thanks.
     
  2. seventeen thread starter macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #2
    Nevermind, figured it out. should have used [] instead of {} in accessing the array element. nur.
     

Share This Page