jonniejoejonson Posted September 2, 2009 Share Posted September 2, 2009 Would you use a class to select rows from a database? i got this far as a function within my database class, however i am stuck as to how i would then public function query($query) { $this->query_result = mysql_query($query, $this->database_connect); while ($row = mysql_fetch_array ($this->query_result)) { ........ ........ } } Normally i would have something like: $id[]=$row["itemId"]; $title[]=$row["itemTitle"]; $description[]=$row["itemDescription"]; in the blank space, but as the rows are going to be different for different queries how can i resolve this issue. Would you infact ever use classes for something like this? Kind regrads J. Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/ Share on other sites More sharing options...
trq Posted September 2, 2009 Share Posted September 2, 2009 Your query method is doing too much. public function query($query) { if ($this->query_result = mysql_query($query, $this->database_connect)) { return true; } } public function fetch() { return mysql_fetch_array ($this->query_result)); } Then you would use it something like.... if ($db->query("SELECT * FROM foo")) { while ($row = $db->fetch()) { // } } Having said that however, as I'm sure you've noticed, it doesn't really gain you anything you don't already have just by using mysql_* Theres really not much point simply wrapping already existing functionality into classes for no reason. If you made a database abstraction layer (ie a class that could access multiple databases through one interface) that would would be more useful. Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/#findComment-910934 Share on other sites More sharing options...
jonniejoejonson Posted September 2, 2009 Author Share Posted September 2, 2009 Thanks for the response Thorpe... As you can tell im fairly new to OOP, and im really struggling to see the point to using OOP... as it doesn't seem to have any practical use. So to confrim are you saying that in this instance: $sql = "SELECT * FROM forums"; $sql_result = mysql_query($sql,$connection); while ($row = mysql_fetch_array ($sql_result)) { $forumId_array[]=$row["forumId"]; $forumTitle_array[]=$row["forumTitle"]; $forumDescription_array[]=$row["forumDescription"];} it is not possible to use a class to run a query and then return the results into various arrays?... or it is pointless?... or both!... regards J Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/#findComment-910942 Share on other sites More sharing options...
trq Posted September 2, 2009 Share Posted September 2, 2009 Its possible, theres just no point. (why you would want three seperate arrays like that is beyond me though anyway... makes little sense). And just because your using classes doesn't mean your programing in an object oriented manor. Theres allot more to it than that I'm afraid. Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/#findComment-910945 Share on other sites More sharing options...
jonniejoejonson Posted September 2, 2009 Author Share Posted September 2, 2009 Thanks again Thorpe.. i don't suppose you could enlighten me as to when in creating an application like this forum would use OOP?... for which parts of the application: eg login?... selecting the forum threads?... i cant see why using include statements isn't more efficient and better. Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/#findComment-910951 Share on other sites More sharing options...
trq Posted September 2, 2009 Share Posted September 2, 2009 Its a massive topic. Have you read the OOP tutorials on our main site? They'll likely help you more than any post here will. Link to comment https://forums.phpfreaks.com/topic/172838-mysql-select-and-classes/#findComment-910953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.