Page 1 of 1

classes question

Posted: Fri Sep 22, 2006 12:14 pm
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?

Posted: Fri Sep 22, 2006 12:16 pm
by feyd
You can't assign data to a while().

Posted: Fri Sep 22, 2006 12:36 pm
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.

Re: classes question

Posted: Fri Sep 22, 2006 12:43 pm
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.

Posted: Fri Sep 22, 2006 12:52 pm
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?

Posted: Fri Sep 22, 2006 1:46 pm
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;
        }

Posted: Fri Sep 22, 2006 1:55 pm
by a94060
ok man,thanks