Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) My connection file (Connection.php)
- class Connection
- {
- function GetConnect()
- {
- $servername = "localhost";
- $dbname = "ladylike";
- $username = "root";
- $password = "";
- try
- {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- //echo "Connected successfully";
- }
- catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- }
- }
- 2) Login.php from where i am calling my common login function
- <?php
- include '../core/connection.php';
- include "../core/base.php";
- $objbase = new Base();
- $ConnObj = new Connection();
- $action = $_REQUEST['action'];
- $email = $_REQUEST['email'];
- $password = $_REQUEST['password'];
- switch($action)
- {
- case "checklogin":
- {
- CheckLogin($email,$password);
- }
- }
- function CheckLogin($email,$password)
- {
- global $objbase;
- global $ConnObj;
- $objbase->Login($email,$password);
- }
- 3) And finally my common function file (base.php)
- <?php
- require_once 'connection.php';
- $ConnObj = new Connection();
- $conn = $ConnObj->GetConnect();
- class Base
- {
- public $conn;
- public $pdo;
- function __construct()
- {
- $this->conn = new Connection;
- $this->pdo = $this->conn->GetConnect();
- }
- function Login($email,$password)
- {
- /*
- Here when i am using my connection fuile directly then it works
- $servername = "localhost";
- $dbname = "ladylike";
- $username = "root";
- $password = "";
- try
- {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- //echo "Connected successfully";
- }
- catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- $stmt = $conn->prepare("SELECT UserSno,UserName,Email,`Password`,Mobile FROM `user` WHERE Active=1 AND UserName = ? AND `Password` = ?");
- $stmt->execute($email,$password);
- $stmt->execute();
- $stmt->fetch();
- */
- // But when i try to use object that time it gives me an error
- //Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp\www\ladylike\core\base.php on line 71
- //( ! ) Error: Call to a member function prepare() on null in C:\wamp\www\ladylike\core\base.php on line 71
- // line 71 mean below line (Yes i am not getting suggestion for prepare when i hit ctrl + space)
- $stmt = $this->pdo->prepare("SELECT UserSno,UserName,Email,`Password`,Mobile FROM `user` WHERE Active=1 AND UserName = ? AND `Password` = ?");
- $stmt->execute($email,$password);
- $stmt->execute();
- $stmt->fetch();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement