Jump to content

[SOLVED] oop and mysql. any tutorials?


Dragen

Recommended Posts

Actually, I made an error in my above post, it is supposed to be Zend_Db, but I cannot edit it.

 

Yes, pdo. There really is no need to use any trumped up db abstraction these days.

 

I disagree. Have you ever looked at Zend_Database and it's children?

What do you suggest then?

Where can I find a good example of a mysql class?

 

Well, Zend_Db_Adapter_Pdo_MySql, but that is a part of Zend Framework and I don't know if you would like something like that.

I must say I haven't had the chance to look into pdo yet, but what's the downside compared to the zend one? also I'm just wanting to get a class that I can use for mysql queries not the whole zend framework..

 

I imagine that an mysql oop wouldn't be too hard to write, I just need a few points in the right direction to start me off. But that's if I need to write one. If pdo does it for me then I wont need to. It depends on how flexible it is to my needs.

I must say I haven't had the chance to look into pdo yet, but what's the downside compared to the zend one?

 

They really are two different things. The Zend one is a wrapper around pdo (and alot more) written in native php while pdo iself is a php extension written in C.

 

I imagine that an mysql oop wouldn't be too hard to write, I just need a few points in the right direction to start me off. But that's if I need to write one. If pdo does it for me then I wont need to. It depends on how flexible it is to my needs.

 

On top of pdo there is also mysqli which is another extension providing a OOP interface to mysql.

 

I disagree. Have you ever looked at Zend_Database and it's children?

 

I've never used therm though I've looked at there code. They seem to abstract the database out a little too far for me. I'm not against using a factory to build a pdo object based on some configuration, but I still like to use native sql for my queries.

thanks for all the feedback!

I'm currently looking into pdo, but I've hit a slight snag. I just copied an example code from the pdo page .php.net (obviously changing the login details):

<?php
try{
  $dbh = new PDO('mysql:host=host;dbname=dbname', 'username', 'password', array(PDO::ATTR_PERSISTENT => true));
  echo "Connected\n";
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $dbh->beginTransaction();
  $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
  $dbh->exec("insert into salarychange (id, amount, changedate) values (23, 50000, NOW())");
  $dbh->commit();
  
}catch(Exception $e) {
  $dbh->rollBack();
  echo "Failed: " . $e->getMessage();
}
?>

 

I keep recieving this errror:

Fatal error: Call to a member function rollBack() on a non-object in C:\Program Files\wamp\www\test.php on line 12

If I remove the rollback line it outputs:

Failed: SQLSTATE[28000] [1045] Access denied for user 'username'@'host' (using password: YES)

what like this?

<?php
try{
  $dbh = new PDO('mysql:host=host;dbname=dbname', 'username', 'password', array(PDO::ATTR_PERSISTENT => true));
  echo "Connected\n";
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(Exception $e) {
  $dbh->rollBack();
  echo "Failed: " . $e->getMessage();
}

try{
  $dbh->beginTransaction();
  $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
  $dbh->exec("insert into salarychange (id, amount, changedate) values (23, 50000, NOW())");
  $dbh->commit();
}catch(Exception $e) {
  $dbh->rollBack();
  echo "Failed: " . $e->getMessage() . '<br />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.