Dragen Posted January 28, 2008 Share Posted January 28, 2008 are there any decent tutorials or anything that teach how to make an oop for a mysql connection, where I can do all of mysql queries through? I've searched google, and here, but can't find anything helpfull. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/ Share on other sites More sharing options...
Highlander Posted January 29, 2008 Share Posted January 29, 2008 Or you can just use the PDO classes. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452115 Share on other sites More sharing options...
Dragen Posted January 29, 2008 Author Share Posted January 29, 2008 PDO? Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452277 Share on other sites More sharing options...
trq Posted January 29, 2008 Share Posted January 29, 2008 Yes, pdo. There really is no need to use any trumped up db abstraction these days. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452289 Share on other sites More sharing options...
Dragen Posted January 29, 2008 Author Share Posted January 29, 2008 okay, thanks. I'll look into that and see how I go! Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452387 Share on other sites More sharing options...
Daniel0 Posted January 29, 2008 Share Posted January 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452643 Share on other sites More sharing options...
Dragen Posted January 29, 2008 Author Share Posted January 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452849 Share on other sites More sharing options...
Daniel0 Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452853 Share on other sites More sharing options...
Dragen Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452869 Share on other sites More sharing options...
trq Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-452970 Share on other sites More sharing options...
Cep Posted January 30, 2008 Share Posted January 30, 2008 thorpe would you suggest PDO over MySQLi if the user only intends to use a MySQL database? Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453473 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453482 Share on other sites More sharing options...
Dragen Posted January 30, 2008 Author Share Posted January 30, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453583 Share on other sites More sharing options...
Dragen Posted January 30, 2008 Author Share Posted January 30, 2008 sorry scrap that Me being a fool. I've just installed the newest version of Wamp, not keeping all of my old settings so the username/password combo was wrong. Sorry! my stupidity yet again! Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453592 Share on other sites More sharing options...
Daniel0 Posted January 30, 2008 Share Posted January 30, 2008 The connection should have a separate try block. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453627 Share on other sites More sharing options...
Dragen Posted January 30, 2008 Author Share Posted January 30, 2008 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453630 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 Kinda, you don't want the rollback because there is nothing done yet to roll back. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453638 Share on other sites More sharing options...
Dragen Posted January 30, 2008 Author Share Posted January 30, 2008 thanks.. which one. The first or second rollback, or both? I'm presuming I need the second as I've processed the sql and need to check it? Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453642 Share on other sites More sharing options...
trq Posted January 30, 2008 Share Posted January 30, 2008 Yeah, keep the second. Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453649 Share on other sites More sharing options...
Dragen Posted January 30, 2008 Author Share Posted January 30, 2008 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88269-solved-oop-and-mysql-any-tutorials/#findComment-453672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.