Foreach statement

<?php
class class1 {
	function printout(){
		$conn = mysql_connect('localhost', 'user', 'pass');
    	        mysql_select_db('db1');
    	        $sql = "SELECT * FROM `table1`";
    	        $result = mysql_query($sql, $conn);
    	        echo "<table>"
    	        echo "<tr>"
                foreach (_ as _){ //rows
                     foreach(_ as _){ //columns
                          echo "<td>"._____."</td>"
                     }
                     echo "</tr><tr>"
                }
                echo "</tr></table>"
          }
    }
?>

I am trying to use foreach for the first time. Could someone guide me thru this please.

Sure you will need to use mysql_fetch_assoc


                while ($row = mysql_fetch_assoc($query)){ //rows
                     foreach($row as $column => $value){ //columns
                          echo "<td>".$column."</td>"
                     }
                     echo "</tr><tr>"
                }

However, I should point out, that the better way to approach this is using PDO as the method you are using is discouraged by PHP.

This only prints out column titles for every data row. Is there a way to printout the data?

Yep, I just simply put the data you had in your example php code and replaced it with the information you indicated, to output the value just echo $value in the same foreach as your echo $column is in.

Thank you for clarifying.

How do I limit the search to $column in $column[‘column3’]=5 written:

$column->column3->5?

For that, you will use a WHERE clause in your SQL Query (I assumed column3 stores an int value)

$sql = "SELECT * FROM `table1` WHERE column3 = 5";

Is it possible to limit the data shown using the:

foreach($table1->$column as $column3 => 5){

or something like that?

Yes, you can, it isn’t ideal, but you can. Here is an example

                while ($row = mysql_fetch_assoc($query)){ //rows
                     foreach($row as $column => $value){ //columns
                          if ($column == 'column3' && $value == 5) {
                               echo "<td>".$column."</td>"
                          }
                     }
                     echo "</tr><tr>"
                }  

How do you write that with the “->” operator?