Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Fatal error: Call to a member function prepare() on a non-object

Hello, I can someone let me know what is the reason I am getting this error. My code is attached.

<?php
class database {
    private $host;
    private $user;
    private $password;
    private $databaseName;
    private $link;
   
    
    function __construct($host,$user,$p<wbr ></wbr>w,$databas<wbr ></wbr>e) {
        $this ->host = $host;
        $this ->user = $user;
        $this ->password = $pw;
        $this ->databaseName = $database;
    }
    
    function connect(){
        $link = new mysqli($this->host, $this->user, $this->password, $this->databaseName);
        if (mysqli_connect_error()){
            die("mysql connection error");
        } 
    }
    
    
}

$db = new database(hostName,userName,passWord,dbName);

Open in new window

function Login($email,$password,$db<wbr ></wbr>){
                 $db = $this->db_obj;
                 
                 if($stmt =$db->prepare("select id,username,password from users where email =? limit 1"))
                 {
                     $stmt->bindParam('s',$emai<wbr ></wbr>l);
                     $stmt->execute();
                     $stmt->store_result();
                     $stmt->bind_result($id,$us<wbr ></wbr>er,$user_p<wbr ></wbr>assword);
                     $stmt->fetch();
                 }

Open in new window


Thank you,
0
aej1973
Asked:
aej1973
1 Solution
 
käµfm³d 👽Commented:
It would probably help to see the full class containing the Login method. Since it appears you are using the special $this, we'd need to see how you are setting up the variable db_obj.
0
 
aej1973Author Commented:
Hello Kaufmed, I am not getting the error: Fatal error: Call to undefined method database::stmt_init() . Attached is my code...

[embed=function class]
<?php
class database {
    private $host;
    private $user;
    private $password;
    private $databaseName;
    private $link;
   
    
    function __construct($host,$user,$pw,$database,$conn=1) {
        $this ->host = $host;
        $this ->user = $user;
        $this ->password = $pw;
        $this ->databaseName = $database;
        if (!empty($database)) $this -> databaseName = $database;
		if ($conn == 1) $this -> connect();
    }
    
    public function connect(){
        $this->link = mysqli_connect($this->host, $this->user, $this->password, $this->databaseName);
        if (mysqli_connect_error()){
            die("mysql connection error");
        } return $this->link;
    }
    
    
}

Open in new window

<?php
    
    include 'mysqli.php';
    include 'functions.php';

    define('hostName', 'localhost');
    define('userName','username');
    define('passWord,'mypass');
    define('dbName','mydb');
    
    $db = new database(hostName,userName,passWord,dbName);
    $fn = new functions();

Open in new window



Thank you.
class Functions {
	var $db_obj;
	
	function __construct(){
		 $this->db_obj = $GLOBALS['db'];
	}
function Login(){
                 $db = $this->db_obj;
                 $user_name = $_POST['email'];
                 $password =  $_POST['password'];
                 $stmt = $db->stmt_init();
                 if($stmt->prepare("select id,username,password from users where email =? limit 1"))
                 {
                     $stmt->bind_param("s",$email);
                     $stmt->execute();                     
                     $stmt->bind_result($id,$user,$user_password);
                     $stmt->store_result();
                     $stmt->fetch();
                 }
                 
             }
        //End Function.    
}//End Class

Open in new window

0
 
Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
Hi,

Try to var_dump($this->db_obj); My guess is that either you don't have stmt_init function in your $db class (because I cannot see this in your class definition) or the object $this->db_obj is not the object of your Database class.

var_dump will give you the information regarding the object and you will be able to figure-out the cause. You can paste the result of var_dump here for further help.
0
 
Slick812Commented:
greetings aej1973, , I have looked at your code in the database class and the functions class. You make a common, but serious Error in code writing, that many mysqli beginners have. You are trying to mix the mysqli "Object oriented style" code and the mysqli  "Procedural style" code, you CAN NOT mix them, and must use the code style for the way you connect to mysqli, as you do with mysqli_connect( ) , which requires ALL code to be in procedural functions, , OR connect with "new mysqli( )"  , which requires ALL code to be in Object methods, please study the TWO different correct code methods in the manual at -
       http://php.net/manual/en/mysqli.prepare.php

you might change the code in your class Functions to something like -
class Functions {
	var $db_link;
	
	function __construct($link){
		 $this->db_obj = $link;
	}
function Login(){
  $email = $_POST['email'];
  $password =  $_POST['password'];
  $uname =  $_POST['user'];
  if ($stmt = mysqli_prepare($this->link, "select id,username,password from users where email =? limit 1")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $email);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
  mysqli_stmt_bind_result($stmt, $id, $user, $user_password);

    /* fetch value */
    mysqli_stmt_fetch($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);

  if ( ($user ==  $uname) && ($user_password == $password) ) {
    return true;
    } else {
    return false;
    }
} else
  {
  echo "mysqli_prepare Failed";
  return false;
  }
                 
             }
        //End Function.    
}

Open in new window

you do not use effective ways to have a php class for a mysqli database, but you first, may need to practice code for mysqli database use, without the PHP class, so you can more clearly see what you CAN and CAN NOT do in mysqli code work.


ask questions if you need more info
0
 
aej1973Author Commented:
need some time to go through this.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now