Solved

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

Posted on 2014-11-23
6
230 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 75

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to dynamically set the form action using jQuery.

816 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

11 Experts available now in Live!

Get 1:1 Help Now