I've created a class that takes output frpm a mySQL query and formats it and returns it.

Here's the class:

```
<?php 
class sql_output_two_rows extends sql {

    function __construct($sql) {
        $this->sql = $sql;
        $this->output = "";
        parent::__construct($this->sql);
        $this->output .=  "<table class='tablenarrow bordered'>\n";
        $this->output .= "<tr>\n";
        for ($f = 0; $f < $this->num_fields; $f++) {
            if($f%($this->num_fields/2) == 0){
                $this->output .=  "<tr>\n";
            }
            if($f>0 && $f%($this->num_fields/2) != (($this->num_fields/2) - 1) || $f == ($this->num_fields - 1)){
                $this->output .= "<th style='border-radius:0px;'>".$this->field_name[$f]."</th>\n";
            }else{
                $this->output .= "<th>".$this->field_name[$f]."</th>\n";
            }
            if($f%($this->num_fields/2) == (($this->num_fields/2) - 1) ){
                $this->output .=  "</tr>\n";
            }
        }
        $this->output .="</tr>\n";
        for ($r = 0; $r < $this->num_rows; $r++) {
            for ($f = 0; $f < $this->num_fields; $f++) {
                if($f%($this->num_fields/2) == 0){
                    $this->output .=  "<tr style='background:#dbe1ef;'>\n";
                }
                $this->output .= "<td>\n";
                if($this->row_array[$r][$f] == ""){
                    $this->row_array[$r][$f]="&nbsp;";
                }
                $this->output .= $this->row_array[$r][$f];
                $this->output .= "</td>\n";
                if($f%($this->num_fields/2) == (($this->num_fields/2) - 1) ){
                    $this->output .=  "</tr>\n";
                }
            }
            $this->output .=  "<tr>\n";
            $this->output .= "<td colspan = '".($this->num_fields/2)."'>\n";
            $this->output .= "<hr>\n";
            $this->output .= "</td>\n";
            $this->output .=  "</tr>\n";
        }
        $this->output .= "</table>\n";
        // print $this->output;
        return($this->output);
    }
}
?>

```

Notice the last two lines of the class. 

I've commented out the line that prints the output. If I uncomment that line, then I call the class thus:

```
new sql_output_two_rows("select * from accounts limit 10");

```

it prints out just fine.

However, if I leave it as it is, and call it thus:

```
$output = new sql_output_two_rows("select * from cameron.accounts limit 10");

print $output . "\n";

```

then I get the following error:

```
Object of class sql_output_two_rows could not be converted to string

```

To overcome this, I have to add this functionality to the class:

```
 public function __toString(){

    return $this->output;

}

```

My question is this: What is happening to make the one work - ie when I print from the class - and the other not - ie when I return the output.

I hope I'm clear enough.