Previous: PHP Authorize.net AIM Interfacing Class
Next: PHP Zip Code Range and Distance Calculation

PHP MySQL Database Class

April 19th, 2005

A class for very basic MySQL database connectivity. Written to reduce redundant code in my own projects aswell as aid in debugging and error reporting during the developement phase. Currently connects to a database, execute external files containing SQL commands, insert and update from an array of key => value pairs, insert and update with sql command, query (one result), query (multiple rows), and dump a select query to a table. The zip file contains the class aswell as a demonstration script.

Download Version 1.0.4: db-1.0.4.zip

Do you like this code? Rate This Script at Hot Scripts.

  Screenshot of the MySQL database class in action


Categories
Web Development PHP

Related Posts


Technorati Tags

28 Responses to “PHP MySQL Database Class”

RSS Subscription Comments RSS Feed

  1. serkan Says:

    how can i make a class to insert data to database in php

  2. Micah Says:

    RE: serkan
    The sample file included with the class shows how this is done.

  3. Rich Says:

    Hi,
    Your script is working for me but I can't find which database was used to create the table users in ! I'm new to MySQL. Could you help me out with this?

  4. Salvi Says:

    Hi,

    The script works perfectly fine on my PC, but online I get a "no database selected" error. I think it has something to do with the MySQL version of the server being quite old (3.23).

    Could you tell me what I should change within the script?

  5. Micah Says:

    Are you sure you typed the database name correctly? Are you sure there is a database setup online?

  6. Andreu Says:

    This database class is a precious piece of php code.

  7. Diego Says:

    Hi Micah, sorry for bother you but i get some rare error..
    Parse error: syntax error, unexpected $end in C:\wamp\www\currency\include\db.class.php on line 458
    Just including the file I get this.. any ideas?
    Best regards and thanks for this..

  8. “PHP Database classes” make my eyes bleed. | For Great Justice Says:

    [...] the maze of duplicate entries I encountered yesterday through StumbleUpon, I came across this page, which reads: A class for very basic MySQL database connectivity. Written to reduce redundant code [...]

  9. prato Says:

    Hey, Micah,

    Thanks for the class... It will be very usefull to me...

    Thank you again!

  10. Raoul Huisman Says:

    Hi Micah,

    Thanks for your MySql Class.
    One question. Is it only working on PHP 5 or also on lower versions ?

    Raoul

  11. James Says:

    Thanks for a very clear tutorial. This is very helpful since I am making my code to OO design.

    more power.

  12. Tapan Bhanot Says:

    Hi,

    Is this still being used ?

    Thanks

  13. nathan king Says:

    Many thanks - this will come in handy. Using it in a new play project I am creating....
    -N@

  14. xyz2did Says:

    Hi,
    thank's your class, and please answer my question...
    how to make query DELETE..
    I was tried :
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/classmysql/db.class.php on line 137

    Many thank's

  15. PHP MySQL Database Class - Monjes - Cultura libre Says:

    [...] PHP MySQL Database Class Esta es una clase muy b

  16. curious Says:

    Good job, a nice handy class, i will use it on my scripts.

    I found kind of bug, at select, when query fails, row_count remains the same, i think it`s better to set it to 0 ;

    if (!$r) {
    $this->last_error = mysql_error();
    $this->row_count = 0;
    return false;
    }

  17. vicky Says:

    hi

    many many thanks

  18. lnh Says:

    Thanks mate

  19. Android Says:

    Thanks a lot. Very useful.

  20. Nandana Says:

    Thanks, You done a Good job.

  21. ijaz Says:

    its very useefull for me.

  22. andres Says:

    excelente muy buena

  23. John Sanders Says:

    I had the same error as Diego: "Parse error: syntax error, unexpected $end in C:\wamp\www\currency\include\db.class.php on line 458"

    The fix on my system was to replace any instance of

  24. AbuMosaab Says:

    Very nice class.

    I would suggest that you consider developing another flavour that uses MySqli.

    As for this one, there is just one missing thing in it, ESCAPING strings. You can add a public function that can be used to escape values before being put into a query.

  25. Matti Says:

    The same error as Diego had:

    C:\Documents and Settings\harmat\Desktop\PHP5>php-5\php.exe DB_esimerkki_luokat
    _testi.php
    PHP Parse error: parse error, unexpected $end in C:\Documents and Settings\harm
    at\Desktop\PHP5\db.class.php on line 457

    Parse error: parse error, unexpected $end in C:\Documents and Settings\harmat\De
    sktop\PHP5\db.class.php on line 457

    C:\Documents and Settings\harmat\Desktop\PHP5>pause
    Press any key to continue . . .

  26. ussher Says:

    of the 3 classes that i have so far needed to start what i want to do, you have built 2 of them!

    Thanks google, and thanks to you for making them.

    cheers.

  27. ussher Says:

    $data = array(
    'id' => '',
    'timestamp' => '',
    'txn_id' => 'ok',
    'verify_sign' => 'verified',
    );
    $id = $db->insert_array('ipns', $data);
    if (!$user_id) $db->print_last_error(false);
    $db->print_last_query();
    $db->dump_query("SELECT * FROM ipns WHERE id=$id");

    produces the insert SQL of:
    INSERT INTO ipns (id,timestamp,txn_id,verify_sign) VALUES (,,'ok','verified')
    wheras it needs to be:
    INSERT INTO ipns (id,timestamp,txn_id,verify_sign) VALUES ('','','ok','verified')

    for the cases where the variable is set, but has no value.

  28. Bobbink - 11 Internet Says:

    Thanks for sharing, keep up the good work!

Leave a Reply