TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-03-2007, 04:43 PM   #1 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default 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?
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-04-2007, 07:50 AM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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.
SOCK is offline  
Reply With Quote
Old 12-04-2007, 07:57 AM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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.
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
ReSpawN (12-04-2007)
Old 12-04-2007, 08:20 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
ReSpawN (12-05-2007)
Old 12-04-2007, 08:23 PM   #5 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default

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.
Swordbeta is offline  
Reply With Quote
Old 12-04-2007, 09:08 PM   #6 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Quote:
Originally Posted by Swordbeta View Post
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.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
ReSpawN (12-05-2007)
Old 12-04-2007, 09:53 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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.
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-04-2007, 10:00 PM   #8 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default

Thanks for your post SOCK :)
Respawn,I'm also dutch :P
Swordbeta is offline  
Reply With Quote
The Following User Says Thank You to Swordbeta For This Useful Post:
ReSpawN (12-05-2007)
Old 12-04-2007, 10:13 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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! ;)
Tanax is offline  
Reply With Quote
Old 12-05-2007, 06:04 AM   #10 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

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. ;)
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:22 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design