svivian Posted August 1, 2008 Share Posted August 1, 2008 At the moment I'm using a set of functions for connecting to a database, running queries and so on. I've heard a few people suggest using classes for this stuff, but examples I've seen are purely functions wrapped in a class, which is pointless - the class doesn't represent an object at all. Are there any good ways to write these type of classes? I've got a few ideas: - when running a query, store the "result resource" in a variable in the class; this helps keep db-specific code (eg mysql functions) out of the main script - store row values internally: probably not worth doing since you generally want the data back every time you get results The problem with storing things internally is that if you have nested query loops (eg looking up more data from the results you just got back) it fails because you overwrite the result resource from the outer loop, with the one in the inner loop. Anyway... thoughts/suggestions welcome Quote Link to comment https://forums.phpfreaks.com/topic/117764-is-it-worth-making-a-database-connection-class/ Share on other sites More sharing options...
Liquid Fire Posted August 1, 2008 Share Posted August 1, 2008 I would not be able to live without my database class, it makes creating certain queries a lot easier and you can automatically clean query values. Now as for storing the query result in the database object, that is not smart and most popular database classes(like PEAR) do don't do that, you would do something like $query = "SELECT a, b, c FROM table_a WHERE a = '1'"; $rows = $database->get_all($query); foreach($rows as $row) { //another set of queries based on first query } Quote Link to comment https://forums.phpfreaks.com/topic/117764-is-it-worth-making-a-database-connection-class/#findComment-605726 Share on other sites More sharing options...
svivian Posted August 3, 2008 Author Share Posted August 3, 2008 ^But that's kinda my point. Wrapping the functions in a class has no use, aside from giving them a pseudo-namespace (which can be done with a function prefix, eg "dbQuery", "dbRow"). It would make sense if you're storing something within the class... Quote Link to comment https://forums.phpfreaks.com/topic/117764-is-it-worth-making-a-database-connection-class/#findComment-606948 Share on other sites More sharing options...
maexus Posted August 3, 2008 Share Posted August 3, 2008 For results I store in the object as well as return them so it gives me options. You can certainly use the many DB objects/abstractions that are out there if you wish. Quote Link to comment https://forums.phpfreaks.com/topic/117764-is-it-worth-making-a-database-connection-class/#findComment-606982 Share on other sites More sharing options...
bombes Posted August 8, 2008 Share Posted August 8, 2008 There are already classes made for this. The one I use is called db_works.php. It's used in osCommerce. I'm sure most open source projects (osCommerce, phpBB, etc...) come with these type of classes. Quote Link to comment https://forums.phpfreaks.com/topic/117764-is-it-worth-making-a-database-connection-class/#findComment-611883 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.