salmander Posted July 2, 2011 Share Posted July 2, 2011 hi 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 getRows() function which extracts the rows from the database and then returns it. here is the 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->arr2 = $this->queryString($sql); //Declaring that $return is an array $return = array(); foreach ($this->arr2 as $key=>$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 then on the other page, i use this code to fetch out everything from the objects returned and display it....but I dont understand whats going wrong! $class = new fetch_assoc; $results = $class->getRows(); //echo out everything foreach ($results as $result) { foreach ($result as $key=>$value) { echo "hello"; echo $key . ": " . $value; }//foreach ($result as $key=>$value) ends here } Ok Quote Link to comment https://forums.phpfreaks.com/topic/240972-need-help-regarding-phpmysql-fetching-out-rows-from-database-using-oop/ Share on other sites More sharing options...
gizmola Posted July 3, 2011 Share Posted July 3, 2011 3 things: - $row = mysql_fetch_assoc($query); That will fetch exactly one row. You would need to while loop if you actually want to get more than the first row of the result set (assuming there is one) - You have an associated array that you are foreaching through to make the exact same thing. It doesn't make any sense. - In my signature there's a link about having problems with mysql that you should take a look at. It's probably relevant to debugging your problem. Quote Link to comment https://forums.phpfreaks.com/topic/240972-need-help-regarding-phpmysql-fetching-out-rows-from-database-using-oop/#findComment-1237832 Share on other sites More sharing options...
salmander Posted July 3, 2011 Author Share Posted July 3, 2011 3 things: - $row = mysql_fetch_assoc($query); That will fetch exactly one row. You would need to while loop if you actually want to get more than the first row of the result set (assuming there is one) - You have an associated array that you are foreaching through to make the exact same thing. It doesn't make any sense. - In my signature there's a link about having problems with mysql that you should take a look at. It's probably relevant to debugging your problem. yes I was using while loop before to get all the rows in the database, but it wasnt working so i thought that let me simplify it and see the problem then later on, I will add the while loop later. And if I am not wrong you use for each loop to iterate through all the elements in the assosiative array. Appreciate your effort in helping me, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240972-need-help-regarding-phpmysql-fetching-out-rows-from-database-using-oop/#findComment-1237847 Share on other sites More sharing options...
gizmola Posted July 3, 2011 Share Posted July 3, 2011 Use a foreach loop. foreach ($rows as $row) { var_dump($row); } Quote Link to comment https://forums.phpfreaks.com/topic/240972-need-help-regarding-phpmysql-fetching-out-rows-from-database-using-oop/#findComment-1237966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.