Archadian Posted October 5, 2008 Share Posted October 5, 2008 can someone give me an example of how to use MySQL DB queries in OOP to store and retrieve info from the tables. Also, I see $db->query() in a few things of code. Is query() a function in OOP or would that be just a function someone wrote? Thanks. Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/ Share on other sites More sharing options...
genericnumber1 Posted October 5, 2008 Share Posted October 5, 2008 OOP is just a style of programming, in the case of someone doing $db->query() they're calling the function query() in the object that $db is an instance of ($db = new Object(). There are many classes (objects) that have mysql functionality... PDO (works for most databases) and Mysqli being two off the top of my head. Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657384 Share on other sites More sharing options...
Archadian Posted October 5, 2008 Author Share Posted October 5, 2008 Is there a way to use a function for all tables or will i have to make a function for each table? Can anyone post some code on how to do this? (i won't copy code, i don't do that, just learning purposes) Thanks. Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657391 Share on other sites More sharing options...
keeB Posted October 5, 2008 Share Posted October 5, 2008 Is there a way to use a function for all tables or will i have to make a function for each table? Your question, unfortunately, shows a fundamental lack of programming knowledge. Jumping in to OO-Development with such a basic skill set will cause more harm than good, in my opinion. While it's always great to branch out and learn a diverse programming vocabulary, you should start with gaining some experience first. To answer your question, it depends on what you're trying to accomplish. If you want to write a function which loads, say, session data related to a user, it could be categorized as 'have[ing] to make a function for each table' However, the real question is, how are you executing this query? Do you do it like this? (Functional) <?php $conn = mysql_connect("localhost", "user", "pw"); mysql_select_db("some_db"); function get_session_data($conn, $userid) { # $conn is the db connection we're using $q = mysql_query("select sessiondata from user", $conn); while($row=mysql_fetch_row($q)) { // do something with the result set } return $data } ?> Or like this? (Functional and a bit more structured) <?php function query($query, $conn) { //build an array of results. $ret = array(); # to be populated and returned $q = mysql_query($query, $conn); while($row=mysql_fetch_row($q)) { $ret[] = $row; //add to the $ret array. } return $ret } function get_session_data($user_id) { $res_array = query("select sessiondata from user where userid ='$user_id'"); $data = $# get some data.... return $data } ?> Or like this? (OOP) <?php class Database { private $connection; public function __contruct($host, $username, $password, $database) { $this->connection = mysql_connect($host, $username, $password); mysql_select_db($database, $connection); } public function query($q) { //build an array of results. $ret = array(); # to be populated and returned $q = mysql_query($query, $this->connection); while($row=mysql_fetch_row($q)) { $ret[] = $row; //add to the $ret array. } return $ret } } class User { private $db_conn; public function __construct() { $this->db_conn = new Database("localhost", "user", "pw", "some_db"); } public function get_session_data() { $this->db_conn->query("select sessiondata from user where userid = '$user_id'"); // do something with the data.. return $data } } ?> Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657421 Share on other sites More sharing options...
Archadian Posted October 5, 2008 Author Share Posted October 5, 2008 How do i gain experience if i don't ask questions like this? I thought gaining experience was learning. I know how to do the db connection i just thought there might be a better way or another way to use one function to connect to the tables i need to connect to. Thank you for the reply Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657519 Share on other sites More sharing options...
keeB Posted October 5, 2008 Share Posted October 5, 2008 I commend you for wanting to learn. Experience comes from not only learning, but putting what you learn to good use which causes you to understand what you've learned. Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657530 Share on other sites More sharing options...
Archadian Posted October 5, 2008 Author Share Posted October 5, 2008 Yeah im the "hands on learner". I mostly do trial and error and learn from the mistakes While keeping security in mind. Link to comment https://forums.phpfreaks.com/topic/127082-database-queries-in-oop/#findComment-657579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.