Shannon R. Posted July 1, 2012 Share Posted July 1, 2012 I'm just trying to implement a simple login system (done this 100's of times) but whilst doing this I'm trying to implement my own framework. So, I have my database class, which has a select function: function select($table, $rows="*", $cond="") { $query = "SELECT ".$rows." FROM ".$table." "; if ($cond != "") { $query .= "WHERE".$cond; } return mysql_query($query); } So, we all know what this does just by looking at the code, and this should work fine until I throw a numrows call into the mix. Since this is actually returning the query result (boolean) I can't actually put it through mysql_num_rows, since the first argument is expected to be a resource. My num_rows function is simple: function num_rows($query) { return mysql_num_rows($query); } The question is, how do I make my select function able to be used in the num_rows function so I can actually log the user in without receiving this error: Regards, Shannon R. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/ Share on other sites More sharing options...
darkfreaks Posted July 1, 2012 Share Posted July 1, 2012 function get_rows ($table_and_query) { $total = mysql_query("SELECT COUNT(*) FROM $table_and_query"); $total = mysql_fetch_array($total); return $total[0]; } Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358292 Share on other sites More sharing options...
Shannon R. Posted July 1, 2012 Author Share Posted July 1, 2012 That doesn't help me understand what I need to do. I need to return the query, not a boolean (result of query) so I can pass it through the num_rows function. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358294 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 We would need to see your entire class. Do you have a class variable that stores the result? I can't help but point out that you're out of step with the current recommended approach, in building a db class on top of the mysql api, when there is mysqli or pdo available, which both support bind variables. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358298 Share on other sites More sharing options...
Shannon R. Posted July 1, 2012 Author Share Posted July 1, 2012 We would need to see your entire class. Do you have a class variable that stores the result? I can't help but point out that you're out of step with the current recommended approach, in building a db class on top of the mysql api, when there is mysqli or pdo available, which both support bind variables. You can view ALL my code, here -- http://github.com/Shazer2/SimpleBlog Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358300 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 There's a number of different ways to address your requirement. Here's one: class DB { private $_numrows = 0; // rest of your class omitted function select($table, $rows="*", $cond="") { $this->_numrows = 0; $query = "SELECT ".$rows." FROM ".$table." "; if ($cond != "") { $query .= "WHERE".$cond; } $result = mysql_query($query); if ($result) $this->_numrows = mysql_num_rows($result); return $result; } function num_rows() { return $this->_numrows; } } Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358303 Share on other sites More sharing options...
Shannon R. Posted July 1, 2012 Author Share Posted July 1, 2012 That makes more sense. I struggle to think logically to come up with solutions like these. My login still isn't working, but I don't think it has anything to do with this. EDIT: On second thought, it must be. I did a little debugging, and all my queries and login code is correct. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358314 Share on other sites More sharing options...
gizmola Posted July 1, 2012 Share Posted July 1, 2012 One of the big benefits of a class is that the class can contain variables as well as methods. In terms of evolution, OOP is a relative addon to procedural programming in a lot of cases -- c and c++ being one example. Originally procedural/function based languages had the ability to make structs or records. What turned them into objects was the addition of methods, so usually people who come to oop from that direction are already comfortable with the idea of storing a bunch of related data in a variable. PHP arrays are a good starting point. It seems the main thing you really want to remind yourself, is that class variables are there for you to stitch together your methods. Starting with setters/getters is a typical approach. It seems you were already pondering this question when you started: "I want a getNumrow() method. How do I implement that?" I just filled in an approach that works in this scenario. Since the value of num_rows is intrinsically tied to a SELECT query, it makes sense for that variable to be set at the time you execute a query. Since it's an internal detail, there is no real value to making the variable itself public. You need a variable to store the result of the mysql_num_rows() call, so you need a class variable. Hopefully this approach will help you solve similar problems as you expand the database class. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358316 Share on other sites More sharing options...
Shannon R. Posted July 1, 2012 Author Share Posted July 1, 2012 One of the big benefits of a class is that the class can contain variables as well as methods. In terms of evolution, OOP is a relative addon to procedural programming in a lot of cases -- c and c++ being one example. Originally procedural/function based languages had the ability to make structs or records. What turned them into objects was the addition of methods, so usually people who come to oop from that direction are already comfortable with the idea of storing a bunch of related data in a variable. PHP arrays are a good starting point. It seems the main thing you really want to remind yourself, is that class variables are there for you to stitch together your methods. Starting with setters/getters is a typical approach. It seems you were already pondering this question when you started: "I want a getNumrow() method. How do I implement that?" I just filled in an approach that works in this scenario. Since the value of num_rows is intrinsically tied to a SELECT query, it makes sense for that variable to be set at the time you execute a query. Since it's an internal detail, there is no real value to making the variable itself public. You need a variable to store the result of the mysql_num_rows() call, so you need a class variable. Hopefully this approach will help you solve similar problems as you expand the database class. That's very logical and I will be taking that exact approach in the near future. For now, my login problem still remains, but I'm not receiving an error. It must have to do with either the username or password. I'll investigate further and respond accordingly. EDIT: Well, for some reason _numrows is returning as 0 and therefore not allowing me to login. I'm not sure why? Code updated on GitHub. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358318 Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2012 Share Posted July 1, 2012 I'm not receiving an error. You must not have php's error_reporting/display_errors set to show all php detected errors. Your process.php page is failing with a fatal runtime error because the $db variable doesn't exist on the process.php page. Web servers are stateless. Other than an entry in the access log file, they don't know or care what happened before the current http request and they don't know or care what will happen after the current http request. Every page that is requested is completely separate from every other page request. Your process.php code must include all the code needed to define and create an instance of your db class before it can use any of the methods or properties in that class. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358359 Share on other sites More sharing options...
Shannon R. Posted July 2, 2012 Author Share Posted July 2, 2012 The db variable is created in global.php, which I have referenced in the appropriate places. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358460 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2012 Share Posted July 2, 2012 Not according to the code you have posted at the github link. Your form submits to process.php. The ONLY code that is in process.php is - <?php if (isset($_POST['username'])) { $username = $_POST['username']; $username = htmlspecialchars($username); $password = $_POST['password']; $password = htmlspecialchars($password); $password = hash("sha512", $password); $query = $db->select("sb_users", "*", "username={$username} AND password={$password}"); $count = $db->num_rows($query); if ($count == 1) { $_SESSION['username'] = $username; header("location: index.php"); } else { echo "Incorrect username or password."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358463 Share on other sites More sharing options...
Shannon R. Posted July 2, 2012 Author Share Posted July 2, 2012 Ah, my apologies. The latest code is there now. Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358479 Share on other sites More sharing options...
Shannon R. Posted July 2, 2012 Author Share Posted July 2, 2012 Solved the problem! Took another look over my db class and realised my select function was incomplete, when concatenating if $cond != "" I didn't put a space after WHERE, when concatenating the $cond. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358486 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.