title: Connecting to Database Using PHP OOP
body: <p>I am developing PHP OOP application, which I need help with. I am developing a class(fetch_assoc) which has a function db_connect. It simply connects to the database. Then I have <code>getRows()</code> function which extracts the rows from the database and then returns it. Here is the code:  </p>
  
  <pre><code>function __construct() {  
       mysql_connect("localhost","root","") or die(mysql_error());  
       mysql_select_db("TestProject") or die(mysql_error());  
  }     
  
  function getRows() {  
       $sql = "SELECT * FROM Record";  
       $this-&gt;arr2 = $this-&gt;queryString($sql);  
       //Declaring that $return is an array  
       $return = array();  
       foreach ($this-&gt;arr2 as $key=&gt;$value) {  
             $return[$key] = $value;  
       }//foreach ends here  
       return $return;  
  }     
  
  function queryString($sql) {  
       $query =  mysql_query($sql);  
       $row = mysql_fetch_assoc($query);  
       return $row;  
  }// function queryString ends here()  
  </code></pre>
  
  <p>Then on the other page, i use below code to fetch out everything from the objects returned and display it... But I dont understand whats going wrong!</p>
  
  <pre><code>$class = new fetch_assoc;  
  $results = $class-&gt;getRows();  
  //echo out everything  
  foreach ($results as $result) {  
      foreach ($result as $key=&gt;$value) {  
      echo "hello";    echo $key . ": " . $value;  
  }//foreach ($result as $key=&gt;$value) ends here  
  </code></pre>