Germstorm
Germstorm

Reputation: 9839

Useful PHP database class

I am working on a small PHP website. I need a MySql database access class that is easy to configure and work with.

Does not need to be a full framework, I only need a max. few classes.

Upvotes: 8

Views: 19626

Answers (11)

Ivan Z
Ivan Z

Reputation: 1592

I recommend to use PHP-MySQLi-Database-Class, which utilizes MySQLi and prepared statements (this means that you will be protected from SQL injection). Class is well documented.

Upvotes: 0

ajporterfield
ajporterfield

Reputation: 1019

+1 for PHP's PDO (PHP Data Objects) extension. I use it alongside a really handy database class that extends PDO. You can find this open-source project on Google's Project Hosting at http://code.google.com/p/php-pdo-wrapper-class.

Upvotes: 0

Edmhs
Edmhs

Reputation: 3731

The simplest and lightweight db class is

http://code.google.com/p/edb-php-class/

 <?php
    $result = $db->q("select * from `users`limit 3");

    foreach($result as $a){
            $a = (object) $a;
            echo $a->id.' '.$a->name.' '.$a->url.' '.$a->img.'</br>';
    }


    $result = $db->line("select * from `users` where id = '300' limit 1");
    echo $result['name']; 
    echo $result['surname']; 



    $name = $db->one("select name from `ilike_pics` where id = '300' limit 1");
    echo $name;
    ?>

Upvotes: 1

Alfred
Alfred

Reputation: 61773

Also digg's PDB, which is a simple PDO wrapper or something can be downloaded from http://code.google.com/p/digg/wiki/PDB

Upvotes: 1

Sirupsen
Sirupsen

Reputation: 2115

I've used this once in a while, It's pretty good! (And has an awesome autoslash feature), it's easily customizable and pretty small, but it had everything I needed. You can probably relatively easy expand it to support caching or whatever you desire.

Good luck finding whatever suits you best. :)

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401002

If you don't have really specific needs, I would recommend you take a look at PDO, which is bundled with PHP >= 5.1.

It's fully object-oriented, facilitates compatibility between DB engines ; and it's integration in PHP as a C extension makes it probably a bit faster than equivalents developped in PHP.

You could also take a look at the mysqli extension, which provides both a function-oriented and an object-oriented interface.
But I'd rather go to PDO, I think...


And, now that you are spending (investing ;-) ) some time looking for new stuff, you could take a look at prepared statements : they are supported by both mysqli and PDO, and are nice for (amongst other things) security reasons (no need to worry about escaping your data to prevent SQL injections)


If you had said you need a full ORM solution, I would have pointed you to Doctrine, which is really a great ORM framework ; but it's probably way too much for your needs...

Upvotes: 4

Pawka
Pawka

Reputation: 2576

You can try Zend_Db from Zend Framework. Later you may include mode components from ZF.

Upvotes: 1

John Carter
John Carter

Reputation: 55271

If you're happy with it being MySql specific, MySqli is the default choice.

Upvotes: 5

Lior Cohen
Lior Cohen

Reputation: 9055

PDO works great for me, even tho it's not a fully blown library like PEAR::MDB2.

PDO is a compiled extension of PHP5, so there's a small performance benefit as well.

Upvotes: 14

Paul Dixon
Paul Dixon

Reputation: 300845

ADODb is pretty easy to work with and worth considering. Some illustrative samples:

  //connect
  $dsn = 'mysql://user:pwd@localhost/mydb'; 
  $db = ADONewConnection($dsn);  

  //get a single value     
  $value=$db->GetOne("select foo from bar where x=?", array($x));

  //get a row
  $row=$db->GetRow("select * from bar where x=?", array($x));

  //easy insert example
  $record=array("id"=>1, "foo"=>"bar");
  $db->AutoExecute("table", $record, "INSERT");  

Upvotes: 10

soulmerge
soulmerge

Reputation: 75704

I think PEAR::MDB2 is what you are looking for.

Upvotes: 4

Related Questions