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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

I use PDO for everything simply because you never know whats in store for the future of your apps.

 

I do all my developement work using sqlite with pdo (because MySql is a hog on my vps's), and can quite easily change to MySql when required.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.