0

ok here is the login function from my class user which parent class is a Db connection file

<?php
require_once("DBConnection.php");
class User extends DBConnection
{   ....

....
...
...
public function Login()
    {
        $sqlSelect = "select  `UserName` from `user` where `UserName` = '$this->userName' and `Password` = '$this->password'";

        $result = @mysql_query($sqlSelect, $this->get_Conn());

//      $dataCount = mysql_num_rows($result);
        if(mysql_num_rows($result) == 0)
        {
            throw new Exception("Login Failed");
        }

        $userData = mysql_fetch_assoc($result);
        extract($userData); 

        $this->userId = $UserId;
        $this->firstName = $FirstName;
        $this->middleName = $MiddleName;
        $this->lastName = $LastName;
        $this->email = $Email;
        $this->userName = $UserName;
        $this->password = NULL;
        $this->loginStatus = true;

        $_SESSION['objUser'] = serialize($this);

        if($remember)
        {
            $struser = serialize($this);
            $expTime = time() + (60*60*24*7);
            setcookie("objUser", $struser, $expTime, "/");  
        }

    }

when from the login page i press login button it just checks for the values inside the box for error and chekcing the array error() its empty it stays on the same page

3
Contributors
9
Replies
30
Views
4 Years
Discussion Span
Last Post by diafol
0

MySQL is not capable of OOP unless you use the PDO wrapper. So you will need to change either to using PDO with MySQL, or go to MySQLi OOP.

Is your DBConnector actually returning a live connection? WHat is the content of that Class?

0

A sample of PDO/MySQL connection with exception catch

try {
    $conn = new PDO('mysql:host=HOST;dbname=DATABASE_NAME', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
0

here is the DB connection class

<?php
abstract class DBConnection
{
    private $conn;

    public function __construct()
    {
        $this->conn = NULL; 
    }

    protected function get_Conn()
    {
        if(is_null($this->conn))
        {
            $this->Set_DBConnection();  
        }

        return $this->conn;
    }

    private function Set_DBConnection()
    {
//      $dbHost = "localhost";
        define("DBHOST", "localhost");
        $dbUser = "raza";
        $dbPass = "pass";
        $dbName = "schoolnpcs";

        $this->conn = @mysql_connect(DBHOST, $dbUser, $dbPass);
        if(!$this->conn)
        {
            throw new Exception("Failed to connect to db server - " .  mysql_error());

        }

        $dbSelect = @mysql_select_db($dbName, $this->conn);

        if(!$dbSelect)
        {
            throw new Exception("Failed to select db" .  mysql_error());
        }

    }
}

?>
0

You need to change to either procedual and carry on using MySQL or change to PDO or MySQLi.

You cannot use OOP with MySQL, this has been covered on a few other threads previously.

Change the usage and you will be fine

0

any book that you could recommend ....im just in the begginer stage of PHP

1

To be honest there are a lot of books out there, although there are also a lot of very helpful tutorials.

It sounds like a cop out but this goolge search:

https://www.google.co.uk/search?client=ubuntu&channel=fs&q=php+oop+for+beginners&ie=utf-8&oe=utf-8&gl=uk&redir_esc=&ei=g9aYUejdOImAOP-dgYAO

Has a lot they you may find very helpful and easy to follow.

As you are starting out, i would shy away from MySQL completely unless you decide to use a PDO wrapper.

If you do get stuck then by almeans drop a thread. The DaniWeb community is second to none when it comes to help and assistance.

Good luck, and happy coding.

0

i have been using many web platforms but dani web is surely the best of all for me no doubt ..... actually i was learning PHP from an institute localy at my place a fast track course and they were teaching MY sql ..... Just let me know that as a pure beginner should i first try the my sql and after im done understanding the basics i could start learning my sqli or PDo wrapper ....because from few days back i knew nothing but html and i do have a wide variety of books with me in PDF and i just looked into them the OOP version they do use my sql i ....... but still what u guys would suggest !!!

1

Leave mysql alone - it's deprecating and may be dropped in the future, so developing with it makes no sense. mysqli has a similar syntax and it can be used procedurally as well as OOPly. PDO provides a wrapper for mysql dbs - note this is not the same as using mysql_* functions - so this may also be useful. I favour PDO, but there's not much to choose between them really.

If you're starting out as you say, then mysqli may be easier - just remember to use parameterized queries so that your input data can be escaped safely.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.