classes question

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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

classes question

Post by a94060 »

Code: Select all

proc.php
<?php
session_start();
class SQL {	
	function login ($username,$password) {
		$this->username = strip_tags($username);
		$this->password = strip_tags($password);
		$this->password = md5($this->password);
		$this->sql = "SELECT `*` FROM `teachers` WHERE `username`= $this->username AND `password`= $this->password";
		$this->query = mysql_query($this->sql);
		$this->result = mysql_num_rows($this->query);
		if ($this->result === 1) {
			//user has been found in the database,lets continue.
			$_SESSION['valid'] = 'TRUE';
			$_SESSION['user'] = $this->username;
			return 'pass';
		}
		else {
			//user was not found in the admin table
			return 'fail';
		}
	}//end of login
	function students ($username) {
		$this->query = "SELECT `*` FROM students WHERE `teacher`= $username";
		$this->run = mysql_query($this->query);	
		
	}
	
}

Code: Select all

index.php
<?php
session_start();
$username = $_SESSION['user'];
include("proc.php");
$get = new SQL;

echo <<<WEL
Welcome to your personal administration panel $username.
You can use this panel to manage all the grades of the students.
WEL;
while ($get->students()) = mysql_fetch_assoc($get->students->run) {
	
}
im trying classes and the problem i get is Parsing Error: syntax error, unexpected '=' (line 11) for index.php.wats the problems?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can't assign data to a while().
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Also, strip_tags() is NOT the way to escape for mysql, mysql_real_escape_string() is.
Additionally, the SQL is wrong:

Code: Select all

`username`= $this->username
should be

Code: Select all

`username`= '$this->username'
It is a good idea to be consistent with your naming:

Code: Select all

$this->sql = "SELECT `*` FROM `teachers` WHERE `username`= $this->username AND `password`= $this->password"; 
$this->query = mysql_query($this->sql);
//BUT
$this->query = "SELECT `*` FROM students WHERE `teacher`= $username"; 
$this->run = mysql_query($this->query);
The line with while is a bit of a mess in more than one way, rethink what you are doing.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: classes question

Post by Christopher »

This is the problem:

Code: Select all

while ($get->students()) = mysql_fetch_assoc($get->students->run) {
Not sure what you are trying to do, probably a getStudents($teacher) method would make sense in the class. Also, "SQL" is a terrible name. This is a Model class, so I would call it TeacherModel or CourseModel or something descriptive.
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

all right all thanks. the thing is,i jus started with classes so im new to them,i was trying to follow the manual-.- and got alittle lost. wat i was trying to do with that while statment was to make a table out of it dsiplaying the data i wanted to show from my db

something like i did w/o classes like

Code: Select all

while($row = mysql_fetch_assoc($query) {
//my actions...
}
anyone can help me?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

function getStudents ($teacher) {
                $this->query = "SELECT `*` FROM students WHERE `teacher`= '$teacher'";
                $students = array();
                $result = mysql_query($this->query)
                if ($result) {
                     while ($students[] = mysql_fetch_array($result, MYSQL_ASSOC)); 
                     mysql_free_result($result);
                }
                return $students;
        }
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok man,thanks
Post Reply