Jump to content

Recommended Posts

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:

sDSt0.png

 

Regards,

Shannon R.

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358298
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358300
Share on other sites

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;
}
}

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358303
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358314
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358316
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358318
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358359
Share on other sites

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.";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/265064-framework-design/#findComment-1358463
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.