Solved

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

Posted on 2014-11-23
6
154 Views
Last Modified: 2016-05-25
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
Comment
Question by:aej1973
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40461466
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
 

Author Comment

by:aej1973
ID: 40461482
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
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 40461555
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40462863
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
 

Author Comment

by:aej1973
ID: 40488112
need some time to go through this.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now