MySQL Handler Class

Consider the scenario where you have only PHP4, you can’t use PEARL DBManager or even a Zend Framework. Using MySQLi extension from PHP is out of question and you can’t update the server because it will affect all other sites.

Well I got into a situation like that today and I had to modify all my MySQLi Handler class to work this around.

I have created a MySQL Handler class. It will work just like the MySQLi Handler class, but using mysql functions.

If you find yourself in a situation just like the one explained above, this might help.

I noticed that the class had a little mistake that would make it not work properly under PHP4 and a little annoying bug that I had on the select method. If you already had downloaded the class, please download this newer version.

Follow the links for downloading the class:

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

17 responses to “MySQL Handler Class

  • Behrouz

    hi my friend
    http://www.flashbanneronline.com
    is a best site for bloger and web mastesers
    you can create your flash banner free online without any ads link on your banner created

    more about site

    Free Flash Banner Generator
    With the help of this service you are able to build a free Flash Banner, SWF file for your personal web page, Weblog or any other advertising intention.
    All services on this site are FREE and you can use the site to create any number of banners you like.
    You can generate your banner in 17 different sizes which embraces almost all standard sizes.
    To generate your banner or flash file you simply go through steps 1-5 and fill the required fields. In Step 5 you can view your banner and generate any changes before downloading.
    You can download your SWF file alone or in Zip file which contains all JavaScript codes and any other debugging codes you need to use your banner in your Website, Weblog.
    Although you do not need any changes in codes but there is a help file inside the zip file which guides you for any manipulation based on your needs.
    Your banner can be run in any browser such as IE, Firefox etc.

    thank you bye

  • chuck

    Thanks for posting this. It is fantastic for me. As a long time C++, c# programmer and not a PHP writer, this class encapsulates almost everything I need to do.

    Why is it typos are always on the first or second line? “The calss is basic”

    thanks again, chuck

  • chuck

    see, even I transposed to characters in the url of my first post

  • mcloide

    Not a problem. I’m doing some changes on the class an probably will release another version. Feel free to get anything that you need from here and if you need some help, post out here and we will find a way to help you out.

  • mcloide

    By the way, I have fixed the typo on the 2nd paragraph of the class. Thanks.

  • chuck

    What is the purpose of the $stmt .=”COUNT(” at about line 180? Being a newbee at PHP I couldn’t figure out what I was doing wrong because always returned the count and not the records when doing a select. I changed it to $stmt .= ” (” so I didnt have to track down the closing parens. Now it works as expected.

  • chuck

    Here is the count change I made to the php5 download. Seems to work for me. Starting at line 181
    try{
    $stmt = “SELECT”;
    $this->stmt = “SELECT”;
    if(is_bool($isdistinct) && $isdistinct === true) {
    $this->stmt .= ” DISTINCT”;
    }
    if(!isset($fields[0])) { // cea
    $stmt .= ” COUNT(“;
    } else {
    $stmt .= ” “; // cea
    } // cea
    if(is_array($fields) && sizeof($fields) > 0) {
    $fieldsSize = sizeof($fields);
    for($k = 0; $k 0) {
    $this->stmt .= “,”;
    }
    $this->stmt .= ” {$fields[$k]}”;
    }
    }
    else {
    $this->stmt .= ” *”;
    }
    if(isset($fields[0])) {
    // $stmt .= “{$fields[0]})”; // cea
    $stmt .= “{$fields[0]}”; // cea
    }
    else {
    $stmt .= “*)”; // cea
    }
    $this->stmt .= ” FROM”;

    Thanks again for posting the class.

  • chuck

    My previous post didnt set $this->$stmt just $stmt everywhere. My bad. but your line 184 if(is_bool($isdistinct) && $isdistinct === true) {
    has 3 equal signs?? is that a bug in php that is not caught or does it mean something?

  • chuck

    Problem with the flds array. The $stmt string is not built with the elements of the $fields array like $this->stmt consequently at the query at 299 only the first field is returned. I have changed my code to use $this->stmt instead of $stmt. Is that correct?

  • mcloide

    Hey Chuck, answering all of your questions:

    I did some changes on the class, fixed some bugs pointed by Chuck and the new version is already available on the server.

    Just clarifying some questions here:

    – The count statement will only be used for the pagination part of the select method. It will determine all the rows that the query, without the pagination since the class statement will be limited for the current page.

    – I do a lot of validation, sometimes, even more that I need, so the is_bool and var === true is to check if the variable being passed is really a Boolean and it’s value is true. I have removed the is_bool since the === will guarantee in a binary safe way that the variable and true are of the Boolean type and the variable have the value equals true.

    Anyway, since you are working with PHP 5, check if your PHP 5 installation supports MySQLi extension. If it does, use the MySQLi Handler class – https://mcloide.wordpress.com/2008/08/29/mysqli-handler-class/ – it is way more stronger and faster than the MySQL one.

    Thanks for pointing out all your doubts and the bugs. I have done an extra check on the method and now everything is fine.

    I’m going to release another version of this class with some modifications for this method and some improvements as well. Sign up into the newsletter that whenever something is changed you will get the notification.

  • chuck

    Installed new php5 version and tested.
    Follows the programmer’s nemesis of “test left and right, up and down. Give it to user and BOOM!

    Here is browser output

    vvvvvvvvvv start test vvvvvvvvvvvvv
    Hello: Page startup still in html
    Hello World3 at 12:38
    After require

    Connected to webdata
    After dbsrc query. I am confused
    Could not query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘FROM manufacturers’ at line 1
    ^^^^^^^ end text ^^^^^^^^^^
    Here is code
    vvvvvvvvvvvvvvvvvvvvv start text

    Hello: Page startup still in html

    <?php

    echo ‘Hello World3 at ‘;
    echo date(“h:i”);
    echo “”;
    // require (“phpinc/MysqlHandler_cea.inc.php”);
    require (“phpinc/MysqlHandler_inc_Stable.php”);
    echo “After require”;
    $user1 = “test”;
    $pw1 = “anybody”;
    $schema1 = “webdata”;
    $dbsrc = new MysqlHandler($user1, $pw1, $schema1);
    echo “Connected to $schema1 ” ;
    $tabs1 = array(“manufacturers”);
    $flds1 = array(“manufacturers_name”,”manufacturers_name”);
    // $rv = $dbsrc->query(“SELECT `manufacturers_name` FROM manufacturers”);
    // echo ( mysql_result($rv, 0,0).””);
    // echo “Count returned $$rv = $rv”;
    $rv = $dbsrc->select($tabs1, $flds1, false);
    echo “After dbsrc query. I am confused”;

    if (!$rv) {
    die(‘Could not query:’ . mysql_error());
    echo “select didnt work” ;
    } else {
    echo “select worked!” ;
    echo “Count result= ” . count($dbsrc->result) . “”;
    etc etc

    ^^^^^^^^^^^^^^^^^^^^^ end text

  • mcloide

    Chuck,

    I have fixed those issues that you are mentioning above. Just to be sure that you have the last stable class, check if the line 33 have a update date and time.

    I have just deleted the file and re-uploaded with a new date and time. It should have the last select method corrected.

    I have included on the zip file a test file that you can use to check how it is working.

    Check it out.

  • chuck

    Congrats! I installed new (Last update: 09/25/2008 4:23pm) and it worked great except of the 575 entries It selected only 10. I don’t understand why pagination is default because I am ignorant of what it is. I set use_pagination = false and it works as I expected. Great work and I am impressed with the prompt responses.

  • mcloide

    Chuck on the test page the pagination is set by default, but you can disable it by setting up the use_pagination flag to false.
    Ex.:
    $mysql = new MysqlHandler(‘root’,’123′,’theschema’,’localhost’);
    $mysql->use_pagination = false;
    $mysql->select($tables = array(‘user’));

    This should do it 🙂

  • chuck

    oh, sorry I was ambiguous. I never used your test case. Looked at it after I tested with my code and posted above. I had already done as you suggested (set default pagination). I do prefer to assure it works with my data and my code, and it did and does after your adjustments.
    Note: I dont do the pretty web stuff. I am the backend guy supplying the data. My requirement for PHP is to update the hosted web server MySql database from our inventory on our local servers.

  • mcloide

    No worries, I just assumed that you have looked to the test case. If you are working only with raw data, then the query method should be the best for you. you can set the query directly to the method and it will return the same result as the select result without pagination, but in a faster way since it doesn’t need to do a lot of for loops to construct the query. If you server is up do date, check if you have the MySQLi extension for PHP installed and if yo do, try using the MySQLi Handler class. The MySQLi extension is better in all aspects even more when you are working with MySQL5. There are things on MySQL 5 that the common MySQL extension does support.
    Anyway glad it helped you. Keep posted I have more libraries to put here.

  • seo blog

    This is a really interesting blog post,I have added your blog to my favourites I really like it,keep up the good work!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: