Results 1 to 11 of 11

Thread: [RESOLVED] [PHP] Database interface

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] [PHP] Database interface

    Hi,

    I'm building a database interface for my CMS.
    Please correct me if I'm wrong: the purpose of such an interface would be that I could easily convert the database engine from one to another.

    I have the following functions the interface:
    • connect
    • close
    • select_db
    • query
    • num_rows
    • fetch_assoc
    • escape_string
    • error
    • errno


    Am I missing anything important/useful?

    Thanks
    Last edited by TheBigB; Sep 21st, 2010 at 02:56 PM.
    Delete it. They just clutter threads anyway.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Database interface

    try taking a look at CodeIgniter http://codeigniter.com/ look through their Database Class in the User Guide ... should give you a pretty good idea of what to look for.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Database interface

    yes -- the purpose of an interface is so that you can define the way a class should work, but leave the actual implementation up to the developer (either you, or someone else). so, then you can create a MySQL-based class and also an Oracle-based class, and all of the method names would be the same.

    I would personally suggest writing methods that allow you to create prepared statements; they are absolutely invaluable and remove the chance of a user opening themselves up to SQL injection.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Database interface

    Have you considered using PDO?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

  6. #6

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Database interface

    Quote Originally Posted by visualAd View Post
    Have you considered using PDO?
    I haven't, but I might, now that I've read that.
    Should I be worried about webhosters not having PDO enabled?

    Quote Originally Posted by kows View Post
    I would personally suggest writing methods that allow you to create prepared statements; they are absolutely invaluable and remove the chance of a user opening themselves up to SQL injection.
    I've never worked with them in PHP but I see PDO has support for prepared queries, so I might consider that.

    Quote Originally Posted by techgnome View Post
    try taking a look at CodeIgniter http://codeigniter.com/ look through their Database Class in the User Guide ... should give you a pretty good idea of what to look for.

    -tg
    After a quick glance I at least found one function I really like;
    Code:
    $data = array(
                   'title' => $title,
                   'name' => $name,
                   'date' => $date
                );
    
    $this->db->insert('mytable', $data);
    
    // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
    PDO would be awesome, but the only question that remains is scale of deployment.
    Thanks for the suggestions so far.
    Last edited by TheBigB; Sep 21st, 2010 at 05:33 AM.
    Delete it. They just clutter threads anyway.

  7. #7

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Database interface

    Also, is it a good idea to design a front-end for PDO?
    Delete it. They just clutter threads anyway.

  8. #8
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Database interface

    May wish to add select_server.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  9. #9

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Database interface

    That's pretty much what connect is for.
    Or do you mean something else?
    Delete it. They just clutter threads anyway.

  10. #10
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Database interface

    Your Interface looked similar to mine. I build the connect string from properties:
    UserID property
    Password property
    DataBase property
    Server Property

    Then my connect method takes no arguments. I thought a server property would be nice if you can change the database. But if you're just connecting to Access then you may not need it.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Database interface

    Quote Originally Posted by TheBigB View Post
    I haven't, but I might, now that I've read that.
    Should I be worried about webhosters not having PDO enabled?
    Web hosts should probably be worried that you'll find another host if they don't support it. I believe that most now do.

    Quote Originally Posted by TheBigB View Post
    Also, is it a good idea to design a front-end for PDO?
    It depends on the design pattern you are using. If you are using something like the model view controller pattern, your front end will be another layer of abstraction on top of the PDO layer. At minimum you should consider a simple templating class for the front end.

    As well as PDO you also have Pear::DB (which is an abstract library similar to PDO) and Mysqli (which supports prepared and paramatized queries). Also, you can see the link in my sig for PHP5 OOP I have created a DB abstraction layer as an example.

    There is little point in writing your though own if there is one that already exists and you don't have very bespoke requirements.
    Last edited by visualAd; Sep 21st, 2010 at 05:55 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width