Jump to content

PHP and Classes


SharkBait

Recommended Posts

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


I use this bit of code lots:

[code]
<?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);
?>
[/code]

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
Link to comment
Share on other sites

[quote]1) Is my logic with this correct?[/quote]
Yes.

[quote]2) How hard is it to create a class I can use and learn from that will do the above[/quote]
Not very.

[quote]3) How's the weather where you are?[/quote]

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.
Link to comment
Share on other sites

I have this:
[code]
<?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
}

?>
[/code]

Would that be the similar thing as

[code]
<?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
}
?>
[/code]
 
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.