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,
aej1973Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aej1973Author Commented:
need some time to go through this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.