Jump to content

mysql select and classes


jonniejoejonson

Recommended Posts

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

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.

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.