Advertisement
Guest User

Call to a member function prepare() on null

a guest
Jan 21st, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. 1) My connection file (Connection.php)
  2.  
  3. class Connection
  4. {
  5.   function GetConnect()
  6.   {
  7.        $servername = "localhost";
  8.        $dbname = "ladylike";
  9.        $username = "root";
  10.        $password = "";
  11.  
  12.        try
  13.        {
  14.             $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  15.             // set the PDO error mode to exception
  16.            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17.             //echo "Connected successfully";
  18.        }
  19.        catch(PDOException $e)
  20.        {
  21.             echo "Connection failed: " . $e->getMessage();
  22.        }
  23.   }
  24. }
  25.  
  26.  
  27. 2) Login.php from where i am calling my common login function
  28.  
  29. <?php
  30. include '../core/connection.php';
  31. include "../core/base.php";
  32.  
  33. $objbase = new Base();
  34. $ConnObj = new Connection();
  35.  
  36.  
  37.  $action  = $_REQUEST['action'];
  38.  $email  = $_REQUEST['email'];
  39.  $password  = $_REQUEST['password'];
  40.  
  41.   switch($action)
  42.   {
  43.      case "checklogin":
  44.      {
  45.          CheckLogin($email,$password);
  46.      }
  47.  
  48.   }
  49. function CheckLogin($email,$password)
  50. {
  51.   global $objbase;
  52.   global $ConnObj;
  53.   $objbase->Login($email,$password);
  54. }
  55.  
  56.  
  57. 3) And finally my common function file (base.php)
  58.  
  59. <?php
  60.   require_once 'connection.php';
  61.  
  62.   $ConnObj = new Connection();
  63.   $conn = $ConnObj->GetConnect();
  64.  
  65.  
  66. class Base
  67. {
  68.     public $conn;
  69.     public $pdo;
  70.  
  71.     function __construct()
  72.     {
  73.       $this->conn = new Connection;
  74.       $this->pdo = $this->conn->GetConnect();
  75.     }
  76.  
  77.    function Login($email,$password)
  78.     {
  79.  
  80.        /*
  81.             Here when i am using my connection fuile directly then it works
  82.  
  83.        $servername = "localhost";
  84.        $dbname = "ladylike";
  85.        $username = "root";
  86.        $password = "";
  87.  
  88.        try
  89.        {
  90.             $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  91.             // set the PDO error mode to exception
  92.            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  93.             //echo "Connected successfully";
  94.        }
  95.        catch(PDOException $e)
  96.        {
  97.             echo "Connection failed: " . $e->getMessage();
  98.        }    
  99.          
  100.         $stmt = $conn->prepare("SELECT UserSno,UserName,Email,`Password`,Mobile FROM `user` WHERE Active=1 AND UserName = ? AND `Password` = ?");
  101.     $stmt->execute($email,$password);
  102.         $stmt->execute();
  103.         $stmt->fetch();
  104.  
  105.  
  106.        
  107.  
  108.          */  
  109.          // But when i try to use object that time it gives me an error
  110.     //Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp\www\ladylike\core\base.php on line 71
  111.         //( ! ) Error: Call to a member function prepare() on null in C:\wamp\www\ladylike\core\base.php on line 71
  112.         // line 71 mean below line (Yes i am not getting suggestion for prepare when i hit ctrl + space)
  113.    
  114.         $stmt = $this->pdo->prepare("SELECT UserSno,UserName,Email,`Password`,Mobile FROM `user` WHERE Active=1 AND UserName = ? AND `Password` = ?");
  115.     $stmt->execute($email,$password);
  116.         $stmt->execute();
  117.         $stmt->fetch();
  118.      }
  119. }
  120.  
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement