PHP OOP Variables and Function Scope ~ Mindboggoling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

PHP OOP Variables and Function Scope ~ Mindboggoling

Post by cs-web »

Hi, I am developing my site and I'm making it oop, cos it rules. But now I'm getting a bit confused. I'm making the user class which has functions like adding and account to mysql, checking a username and password to see if a users logged in.

In this loginvalid function which checks a user is logged in by finding the num of rows with username and pass supplied. (Should only be 1) It connects to the mysql to check this. To do this I connect to mysql with a connection string in connect.php page which looks like this. (User, Pass values changed)

Code: Select all

<?
$connect = mysql_connect ("localhost", "user", "******");
$dbname = "csweb_csweb";
?>

I then have includes.php which has the following.

Code: Select all

<?php
include "connection.php";
include "user/user-class.php";
include "display/display-class.php";
?>

Then in user-class.php which im trying to get a mysql connection working in I have.

Code: Select all

<?
class user
{
function loginvalid($username, $password)
{
$loginquery = "SELECT * FROM users WHERE usernarrme='$username' AND password='$password' ";
print "$GLOBALS[dbname]";
$loginresult = mysql_db_query($GLOBALS[dbname], '$loginquery', $GLOBALS[connect]);
$loginvalid = mysql_num_rows($loginresult);
if ($loginvalid == "1") {
return 1;
}
else {
return 0;
}
}
}
$user = new user;
?>
But when I have a page which uses the login valid function I get.

csweb_csweb
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/csweb/public_html/user/user-class.php on line 9
0

Its printing the dbname fine which confuses me why it wont connect as the connect variable should also work...

Also how can a classes function be accessed in another classes function is it possible.

Please please please help, Chris
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

variables won't get interpolated when they are used in single quotes. You would need to omit single quotes on 8th line of user-class.php (second arg of mysql_db_query function).
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

I did this and now have

Code: Select all

<?
class user
{
function loginvalid($username, $password)
{
$loginquery = "SELECT * FROM users WHERE usernarrme='$username' AND password='$password' ";
print "$GLOBALS[dbname]";
$loginresult = mysql_db_query($GLOBALS[dbname], $loginquery, $GLOBALS[connect]);
$loginvalid = mysql_num_rows($loginresult);
if ($loginvalid == "1") {
return 1;
}
else {
return 0;
}
}
}
$user = new user;
?>
But got the same error...

Thanks, Chris
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

use mysql_error to discover where it goes wrong :)

personally i would use select count(*) as count from user where ...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

usernarrme :?:
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

feyd wrote:usernarrme :?:
By jove you've got it!! THANKS SOOOO MUCH!!! :D

Do you guys no the answer to my second question, how do you use a different function from a different class inside a function?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

within the same class, if you have an instance of said class:

Code: Select all

$this->functionname()
If the class is a static class, then

Code: Select all

classname::functioname()
If the function is in another class instance

Code: Select all

$object->functionname()
provided $object is an instance of that class.
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

What is a static class??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically, it's a class that doesn't require an instance to operate. If you look at the Code Snippets board under "Security: SHA256 hashing algorithm" The code of the first post there is a static class.
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

so for when I have the user class with login valid function.

And I want to use it in render function in display class. I would just use $user->loginvalid() where I want to use it. And it doesnt need any global stuff?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest you create a database abstraction layer that's a singleton or static class.

A singleton is a special instance of a class that only allows 1 to exist. If you want to know more about them, search the boards for it.
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

oh dam it it why did it have to get so hard when it was going so easy :(

Update: I didnt really get and still dont get what a static class is? butI just tried using user::loginvalid() in the other function and it seems to be working fine :D :D :D ! hurrah
Post Reply