output help

Discussion in 'PHP' started by aaron_nimocks, Oct 13, 2006.

  1. #1
    Alright I am confused. Here is what I got

    
    <?php
    
    $sql = "SELECT * FROM category WHERE parent = 0";
    $result = mysql_query($sql) or die(mysql_error());
    while ( $row = mysql_fetch_array($result) )
    	
    	{
    	$output .= "$row->title";
    	}
    
    	return $output;
    
    ?>
    PHP:
    That isnt working, but it does work if I do this.

    
    <?php
    
    $sql = "SELECT * FROM category WHERE parent = 0";
    $result = mysql_query($sql) or die(mysql_error());
    while ( $row = mysql_fetch_array($result) )
    	
    	{
    	echo $row->title;
    	}
    
    ?>
    PHP:
    Any idea why? I really need to use it in the first example.

    Thanks in advance. :)
     
    aaron_nimocks, Oct 13, 2006 IP
  2. Dejavu

    Dejavu Peon

    Messages:
    916
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use $output .= "{$row->title}";

    php must parse the variables in the double quotes, it does not know if the '-' is part of the variable without the brackets.
     
    Dejavu, Oct 13, 2006 IP
  3. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #3
    tried

    $output .= "{$row->title}";

    and didnt work. I have another script that I did it without the brackets and it works just fine. Not sure what I am missing here.
     
    aaron_nimocks, Oct 13, 2006 IP
  4. Dejavu

    Dejavu Peon

    Messages:
    916
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If the second example of you worked, this must work
    <?php

    $sql = "SELECT * FROM category WHERE parent = 0";
    $result = mysql_query($sql) or die(mysql_error());
    $output = '';
    while ( $row = mysql_fetch_array($result) )
    {
    $output .= $row->title;
    }
    echo $output;



    ?>

    otherwise maybe use $row['title']; (it is an array after all)
     
    Dejavu, Oct 13, 2006 IP
  5. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #5
    Figured it out.

    I was using "return"

    Found out you have to be in a function for that to work. :)
     
    aaron_nimocks, Oct 13, 2006 IP