TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Rookietime : mySQL Query class (http://www.talkphp.com/advanced-php-programming/1584-rookietime-mysql-query-class.html)

ReSpawN 12-03-2007 04:43 PM

Rookietime : mySQL Query class
 
Hey guys!

Today I sat down for a nice day of PHP'n and this is what first spiked my attention. The mySQL Query function inside a class. Since I use PHP 4.4.7 (still need to upgrade host lol), I am NOT using objects (to my knowledge).

So, if this post is not good enough for advanced PHP programming, you are allowed to move it. Therefor, don't hate me for my mistakes IF I made them. :P

PHP Code:

class MySQLdb {
    function 
Query($Query){    
    
$this->loadstart[] = microtime();

        
$Query mysql_query($Query) or die( $this->Errormysql_error() ) );
    
        
$this->loadend[] = microtime();
        
        return 
$Query;
    }

    function 
Debug($DebugType){

        if (
$DebugType == 'TimeQ') {
            for (
$i 0$i count($this->loadstart); $i++){
                
$starttime $this->loadstart[$i] + $starttime;
            }
            for (
$i 0$i count($this->loadend); $i++){
                
$endtime $this->loadend[$i] + $endtime;
            }
            
$querytime round(($endtime $starttime), 7);
            return 
$querytime;
        }
            

    } 

Which exports to my page (there is more lines of code but they don't matter as much);

"1 queries made in 0.000324 seconds :: Page loaded in 0.0004311 seconds"

What do you guys think? Is it good, can it be done better? Easier?

SOCK 12-04-2007 07:50 AM

I suppose it's fine, if that's what you want to do. The Query() method is pretty confined to strictly a) timing the query, b) performing the query and returning the Result Resource and c) killing the script if the query fails. These should be three seperate entities. The Query() method should do nothing but perform a dynamic SQL query. There should be a seperate mechanism to determine what happens to the object or script if the query fails. IMHO, an object should never be allowed to kill the script or perform any logic outside itself.

A word about variables. It's a bit confusing to have the method name Query, the argument named Query, an internal variable and return named... well you get the idea. It might make more sense to name the argument $sql, the return value $Result, etc. Just some constructive criticism, take it for what it's worth.

You're using an object; PHP 4 supports objects & classes (thus your ability to define a class), it just doesn't offer the support that PHP 5 does. Still, using an object is simple, the trick is to infuse polymorphism into your class design.

Tanax 12-04-2007 07:57 AM

Don't you have to declare the variable "loadstart" for example in the beginning of your class? Or don't you have to do that in PHP 4?

I just know that in PHP 5, we have to use:
PHP Code:

private $loadstart = array(); 

And so, you should have to use:

PHP Code:

var $loadstart = array(); 

Correct me if I'm wrong.

SOCK 12-04-2007 08:20 PM

Tanax> Good point. Yes, you need to declare all object vars in PHP 4 just as you would with v5, and you're correct, you'd use the 'var' keyword. I'd have to go back to the manual to check, but it may be possible that v4 is so weakly typed (esp. when it comes to class declarations) that you may not even be required to declare the variable beforehand, it may simply be assumed that if it's not in the local scope (i.e. the method scope) that it's an object variable. I always declare variables up front so I've not tested this.

It's also possible we've only seen an abbreviated section of the actual code.

Swordbeta 12-04-2007 08:23 PM

Looks nice al though I don't really get what it means.
But can please someone explain me the variable $this?!
And I also don't understand the -> operator.

SOCK 12-04-2007 09:08 PM

Quote:

Originally Posted by Swordbeta (Post 5272)
Looks nice al though I don't really get what it means.
But can please someone explain me the variable $this?!
And I also don't understand the -> operator.


$this refers to the current object instance. The pointer simply allows the connection between object to method or data.

Classes & Objects (PHP v4)
Classes & Objects (PHP v5)

Aside from the PHP manual explanation, there are alot of good object oriented tutorials out there. Take a look at some Java tutorials to get a feel for object design and properties.

ReSpawN 12-04-2007 09:53 PM

Sock,

It indeed is a abbreviated section of the actual code. Just like you said. Although, you are almost right everywhere in your post. I believe that do do not have to declare var $var before you begin. Since PHP 5 (like I read) uses private, public and others to declare a pre-set var, it taked the edge off of PHP 4. I am still a rookie when it comes to PHP 5 and OOP, since a lot of servers still use the old PHP 4.4.7 and some a bit higher. Since you've got to update phpMyAdmin AND PHP on a global scale, some buyers' scripts might malfunction. That'll be pretty bad for business don't you think? Anyways, I am still going through with my initial script, posted in my first post.

Although I do not have a good grasp of PHP 5 OOP, I am dedicated to learn. So if you got some examples for me to clarify the code a little, gimme some tips and here and there, a peek into some of your script, that would be great!

Oh yeah, I am dutch, so don't mind any typo's I made. :)

Thanks.

[edit]
Oh yeah, is the code for generating a TOTAL QUERY TIME at the bottom of the page correct? The main idea is, that is counts EVERY query's time to process, adds it up and then displays it. Same function can be used with an array to call up all the functions or simply to COUNT them. For example;

"The page processed #queries, it took #seconds and the total page loading time whas #seconds". Every #seconds has a microtime(); exploded and added up ($time[0] + $time[1]) and then displayed.

Swordbeta 12-04-2007 10:00 PM

Thanks for your post SOCK :)
Respawn,I'm also dutch :P

Tanax 12-04-2007 10:13 PM

I've just seen in tutorials that I've read(that were posted when PHP 4 was used), everyone uses var $var, so I'm just assuming that you should use it.

However, I'm using PHP 5, and haven't really checked if that is correct.

You should really upgrade to PHP 5, as it's so much better!
Best of luck with your script, and read the other comments! ;)

ReSpawN 12-05-2007 06:04 AM

I'm gonna switch hosts in a month or so. 24 euro's per year, .eu, php 5.2.5 and a LOT more. Disadvantage of PHP 5 is, that you can script OOP, but it's hardly compatible when you install it on PHP 4. Well, I'll keep you guys updated. ;)

@ Swordbeta :) Woot, not the only dutchie here. ;)


All times are GMT. The time now is 08:27 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0