prob with class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

prob with class

Post by forgun »

Code: Select all

<?
class dataset &#123;
        var $sets = array();
        function pros() &#123;
                $this->info =
                array(
                );
                include("dbs.php");
                $lk = mysql_connect($serv,$user,$pass) or die(mysql_error());
                mysql_select_db($dbname&#1111;0],$lk);
                $qur = "select * from pollqa order by id desc limit 1";
                $res = mysql_query($qur) or die ( "Error display \n" .mysql_error());
                $row = mysql_fetch_assoc($res);
                $this->sets&#1111;0] = $row&#1111;'qs'];
                $this->sets&#1111;1] = $row&#1111;'ans1'];
                $this->sets&#1111;2] = $row&#1111;'ans2'];
                $this->sets&#1111;3] = $row&#1111;'ans3'];
                $this->sets&#1111;4] = $row&#1111;'ans4'];
                $this->sets&#1111;5] = $row&#1111;'ans5'];
                $this->sets&#1111;6] = $row&#1111;'ans6'];
                return $this->sets;
        &#125;
&#125;
?>
i have prob with the area

Code: Select all

$row = mysql_fetch_assoc($res);
                $this->sets&#1111;0] = $row&#1111;'qs'];
                $this->sets&#1111;1] = $row&#1111;'ans1'];
                $this->sets&#1111;2] = $row&#1111;'ans2'];
                $this->sets&#1111;3] = $row&#1111;'ans3'];
                $this->sets&#1111;4] = $row&#1111;'ans4'];
                $this->sets&#1111;5] = $row&#1111;'ans5'];
                $this->sets&#1111;6] = $row&#1111;'ans6'];
                return $this->sets;
it's not give me any kind of data
why is that?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

is $res giving you any data? try checking it with mysql_num_rows to see if you are getting anything.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

This really isn't something you want to do. By wrapping this code up in a class you are guaranteeing that it will be at least 20% slower! I'm not saying "Don't use a class!". The problem is that this is not good class design. Your best bet would be to notice that in the code provided there are numerous areas of responsibility. Create a class for each of those. For example...

1) a class to handled the db connection.
2) a class to handle queries
3) a class to handle your sets of information.

Perhaps something like...

Code: Select all

if($db_connection->doConnect()==true)
   { 
   if($db_query->doQuery()==true)
      { 
      while($row=$db_query->fetchRow())
         { $setsClass->processRow($row); }
      }
   else
      { /* Error handling */ }
   }
else
   { /* Error handling */ }
This way, your db connection handling object could be swapped out if you should change databases. Also, what if you need to change how the sets data is accessed or dealt with? It can be changed without risk of harming any of the other code.

Another thing. Performing operations against referenced variables ($this) is 10 to 20 percent slower than non-referenced vars. Therefore, all of those "$this->sets[]=$row[]"
statements has got to be slow as Christmas. For the sake of speed, you'd be better off to make $sets just a plain old array in the global scope. Another option is to create a sets class and update/add it's elements directly. Not good OO practice, but I'm talking about treating the class more like a struct. I do this kind of thing all the time and the speed difference is considerable.

Anyways, check out the Eclipse library (http://www.students.cs.uu.nl/people/voo ... /index.php). In my opinion, it is the best designed OO lib out there! Not extemely feature rich, but that makes it perfect for what most people are doing.

Cheers,
BDKR
Post Reply