Jump to content


Photo

PHP and Classes


  • Please log in to reply
2 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 18 August 2006 - 11:14 PM

Yes this has been hit around billions of times but this is my question..


I use this bit of code lots:

<?php
$strqry = "SELECT * FROM blah WHERE blah1 = blah2";
$query = mysql_query($strqry) or die("MySQL Error: <br />{$strqry}<br />". mysql_error());
$result = mysql_fetch_array($query, MYSQL_ASSOC);
?>

Now other things I might also do with my queries is like mysql_num_rows() or mysql_affected_rows, might Delete an entry or three or update a row etc.

Would it be best to come up with my own class that I could use and manipulate the arguments. Like $strqry or something?

I use classes with my VB.Net projects but I want to get into using them more with PHP.  That and I think it will also allow me to clean up my code since alot of my sql queries are just repeated over and over and over.  Why type if all out again?

So.. my question(s):

1) Is my logic with this correct?
2) How hard is it to create a class I can use and learn from that will do the above
3) How's the weather where you are?

=-)  I just want to get more into the OOP part of PHP like I did with VB.NET because it makes things soo much easier if they are repeative bits of coding I need to use.

Thanks


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 August 2006 - 01:39 AM

1) Is my logic with this correct?

Yes.

2) How hard is it to create a class I can use and learn from that will do the above

Not very.

3) How's the weather where you are?


Hot, and humid.

I sometimes use a mysql abstraction class from this website:  http://jaws.townsville.nl/, called ActiveRecord.  Most of the time I will write my own, however, this one is pretty good in my opinion.  If nothing else, you can use it as an example to learn something for your class.

phpclasses.org also has about a million mysql classes that you can take a look at.

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 21 August 2006 - 07:00 PM

I have this:
<?php

class MySQL {
   var $rows;
   var $length;
 
   function doQuery($string) {
      $query = mysql_query($string) or die("MySQL Error: <br /> {$strqry}<br />". mysql_error());
      $this->$rows = mysql_fetch_array($query, MYSQL_ASSOC);
      $this->$length = mysql_num_rows($query);
    }
}

// then to use it would I:

$myQuery = &new MySQL;

$strqry = "SELECT * FROM BLAH";
$myRows = $myQuery->doQuery($strqry);

if($myRows->length < 0) {
 // Nothing found
} else {
 // Found something
}

?>

Would that be the similar thing as

<?php
$strqry = "SELECT * FROM Blah";
$query = mysql_query($strqry) or die("MySQL Error: <br /> {$strqry} <br />". mysql_error());

$result= mysql_fetch_array($query, MYSQL_ASSOC);
$num = mysql_num_rows($query);

if($num <0) {
  // Nothing returned
} else {
  // Found something
}
?>
 
 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users