Page 1 of 1

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

Posted: Tue Jun 26, 2012 8:24 am
by joepiooo1988
Hello,

I got this error and I cant find the problem. This is my Class:

Code: Select all

	public function GetPostcode($type, $postcode=NULL, $huisnr=NULL)
	{
		switch($type)
		{
			case "4XY":
				$sql = "SELECT wijkcode, rd_x, rd_y, lat, lon FROM `postcode`.`4XY`";
				break;
			case "5XY":
				$sql = "SELECT wijkcode, letter, rd_x, rd_y, lat, lon FROM `postcode`.`5XY`";
				break;
			case "6XY":
				$sql = "SELECT wijkcode, lettercombinatie, rd_x, rd_y, lat, lon FROM `postcode`.`6XY`";
				break;
			case "HuisnummerXY":
				$sql = "SELECT nummeraanduidingid, wijkcode, lettercombinatie, huisnr, huisnr_toevoeging, rd_x, rd_y, lat, lon FROM `postcode`.`huisnummerXY`";
				break;
		}
		if(!empty($postcode))
		{
			switch($type)
			{
				case "4XY":
					$sql .= " WHERE wijkcode=:postcode";
					break;
				case "5XY":
					$sql .= " WHERE wijkcode=:postcode AND letter=:lettercomb";
					break;
				case "6XY":
					$sql .= " WHERE wijkcode=:postcode AND lettercombinatie=:lettercomb";
					break;
				case "HuisnummerXY":
					$sql .= " WHERE wijkcode=:postcode AND lettercombinatie=:lettercomb AND huisnr=:huisnr";
					break;
			}
		}
        try
        {
            $stmt = $this->db->prepare($sql);
			if(!empty($postcode))
			{
				$stmt->bindParam(":postcode", substr($postcode, 0, 4), PDO::PARAM_INT);
				switch($type)
				{
					case "4XY":
						break;
					case "5XY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 1), PDO::PARAM_STR);
						break;
					case "6XY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 2), PDO::PARAM_STR);
						break;
					case "HuisnummerXY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 2), PDO::PARAM_STR);
						$stmt->bindParam(":huisnr", trim($huisnr), PDO::PARAM_INT);
						break;
				}
			}
            $stmt->execute();
            $this->postcodelijst = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();

            return $this->postcodelijst;
        }
        catch (Exception $e)
        {
            die ($e->getMessage());
        }
	}
Maybe a fresh look will help me out...

Thanks:)

Re: Fatal error: Call to a member function bindParam() on a

Posted: Tue Jun 26, 2012 8:33 am
by social_experiment
Could you paste the complete error? It is most likely a method (bindParam()) that is being called by a non-object; the error will also give a line number to help narrow it down.

Re: Fatal error: Call to a member function bindParam() on a

Posted: Tue Jun 26, 2012 8:58 am
by Celauran
Here's the section it's complaining about:

Code: Select all

        try
        {
            $stmt = $this->db->prepare($sql);
            if(!empty($postcode))
            {
                $stmt->bindParam(":postcode", substr($postcode, 0, 4), PDO::PARAM_INT);
What does var_dump($stmt) produce? var_dump($this->db)?

Re: Fatal error: Call to a member function bindParam() on a

Posted: Wed Jun 27, 2012 1:15 am
by joepiooo1988
Hello,

Thanks for the reply. I did this in my code:

Code: Select all

$stmt = $this->db->prepare($sql);
if($stmt == false) {
   throw new Exception($this->db->errorInfo()."\n".$sql);
}
And I got this output

Array SELECT wijkcode, lettercombinatie, rd_x, rd_y, lat, lon FROM `postcode`.`6XY` WHERE wijkcode=:postcode AND lettercombinatie=:lettercomb

Still dont know what is going wrong... Anybody see what is wrong?

Re: Fatal error: Call to a member function bindParam() on a

Posted: Wed Jun 27, 2012 1:52 am
by social_experiment

Code: Select all

<?php
$stmt = $this->db->prepare($sql);
?>
The results show that $stmt isn't an object; Is the code you pasted the complete class?

Re: Fatal error: Call to a member function bindParam() on a

Posted: Wed Jun 27, 2012 2:30 am
by joepiooo1988
Hmmm no this is the whole class:

Code: Select all

<?php

include_once "connect.class.php";

class postcode extends connect
{   
    /**
     * De postcodelijst
     * 
     * @var object
     */
    private $postcodelijst;
	
	/**
     * De postcodes opvragen uit de database
     * 
	 * @param string $type
	 * @param string $postcode
	 * @param int $huisnr
     * @return object 
     */
	public function GetPostcode($type, $postcode=NULL, $huisnr=NULL)
	{
		switch($type)
		{
			case "4XY":
				$sql = "SELECT wijkcode, rd_x, rd_y, lat, lon FROM `postcode`.`4XY`";
				break;
			case "5XY":
				$sql = "SELECT wijkcode, letter, rd_x, rd_y, lat, lon FROM `postcode`.`5XY`";
				break;
			case "6XY":
				$sql = "SELECT wijkcode, lettercombinatie, rd_x, rd_y, lat, lon FROM `postcode`.`6XY`";
				break;
			case "HuisnummerXY":
				$sql = "SELECT nummeraanduidingid, wijkcode, lettercombinatie, huisnr, huisnr_toevoeging, rd_x, rd_y, lat, lon FROM `postcode`.`huisnummerXY`";
				break;
		}
		if(!empty($postcode))
		{
			switch($type)
			{
				case "4XY":
					$sql .= " WHERE wijkcode=:postcode";
					break;
				case "5XY":
					$sql .= " WHERE wijkcode=:postcode AND letter=:lettercomb";
					break;
				case "6XY":
					$sql .= " WHERE wijkcode=:postcode AND lettercombinatie=:lettercomb";
					break;
				case "HuisnummerXY":
					$sql .= " WHERE wijkcode=:postcode AND lettercombinatie=:lettercomb AND huisnr=:huisnr";
					break;
			}
		}
        try
        {
            $stmt = $this->db->prepare($sql);
			if($stmt == false) {
  			throw new Exception($this->db->errorInfo()."\n".$sql);
			} 
			if(!empty($postcode))
			{
				$stmt->bindParam(":postcode", substr($postcode, 0, 4), PDO::PARAM_INT);
				switch($type)
				{
					case "4XY":
						break;
					case "5XY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 1), PDO::PARAM_STR);
						break;
					case "6XY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 2), PDO::PARAM_STR);
						break;
					case "HuisnummerXY":
						$stmt->bindParam(":lettercomb", substr($postcode, 4, 2), PDO::PARAM_STR);
						$stmt->bindParam(":huisnr", trim($huisnr), PDO::PARAM_INT);
						break;
				}
			}
            $stmt->execute();
            $this->postcodelijst = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();

            return $this->postcodelijst;
        }
        catch (Exception $e)
        {
            die ($e->getMessage());
        }
	}
	
	/**
	 * Adressen toevoegen aan database
	 *
	 * @param string $database
	 * @param string $naam
	 * @param string $straat
	 * @param string $postcode
	 * @param string $plaats
	 * @param string $telefoon
	 * @param string $website
	 * @param string $bedrijf
	 * @param int $rd_x
	 * @param int $rd_y
	 * @param mixed $lat
	 * @param mixed $lon
	 * @return mixed
	 */
	public function addAdres($database, $naam, $straat, $postcode, $plaats, $telefoon, $website, $bedrijf, $rd_x, $rd_y, $lat, $lon)
	{
		$sql = "INSERT INTO ".$database." (locatie_naam, locatie_straat, locatie_postcode, locatie_plaats, locatie_telefoon, locatie_website, locatie_bedrijf, locatie_rd_x, locatie_rd_y, locatie_lat, locatie_lon) VALUES (:naam, :straat, :postcode, :plaats, :telefoon, :website, :bedrijf, :rd_x, :rd_y, :lat, :lon)";
	
		try
        {
            $stmt = $this->db->prepare($sql);
			$stmt->bindParam(":naam", $naam, PDO::PARAM_STR);
			$stmt->bindParam(":straat", $straat, PDO::PARAM_STR);
			$stmt->bindParam(":postcode", $postcode, PDO::PARAM_STR);
			$stmt->bindParam(":plaats", $plaats, PDO::PARAM_STR);
			$stmt->bindParam(":telefoon", $telefoon, PDO::PARAM_STR);
			$stmt->bindParam(":website", $website, PDO::PARAM_STR);
			$stmt->bindParam(":bedrijf", $bedrijf, PDO::PARAM_STR);
			$stmt->bindParam(":rd_x", $rd_x, PDO::PARAM_INT);
			$stmt->bindParam(":rd_y", $rd_y, PDO::PARAM_INT);
			$stmt->bindParam(":lat", $lat);
			$stmt->bindParam(":lon", $lon);
            $result = $stmt->execute();
            $stmt->closeCursor();

            if($result == 1) { return true; }
			else { return $stmt->errorinfo(); }
			
        }
        catch (Exception $e)
        {
            die ($e->getMessage());
        }
	}
	
	/*
	 * Vraag de adressen op uit de database
	 *
	 * @param string $database
	 * @param mixed $lat
	 * @param mixed $lat1
	 * @param mixed $lat2
	 * @param mixed $lon
	 * @param mixed $lon1
	 * @param mixed $lon2
	 * @param int $afstand
	 * @return mixed
	 */
	public function getAdressen($database, $lat, $lat1, $lat2, $lon, $lon1, $lon2, $afstand)
	{
		$sql = "SELECT * FROM ".$database." WHERE (locatie_lat BETWEEN ".$lat2." AND ".$lat1.") AND (locatie_lon BETWEEN ".$lon2." AND ".$lon1.") HAVING (6371 * acos(sin(radians(".$lat.")) * sin(radians(locatie_lat)) + cos(radians(".$lat.")) * cos(radians(locatie_lat)) * cos(radians(locatie_lon) - (radians(".$lon."))))) <= ".$afstand."";

		try
        {
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();
		
            return $result;
        }
        catch (Exception $e)
        {
            die ($e->getMessage());
        }
	}
		
	/**
	 * Bereken afstand in om trek
	 *
	 * @param float $lat
	 * @param float $lon
	 * @param int $afstand
	 * @return array($lat1, $lat2, $lon1, $lon2)
	 */
	public function calcBoundingBox($lat, $lon, $afstand)
	{
		$radius = 6371.0; // radius omtrek van aarde in km
		$berekenafstand = $afstand / $radius;
		$latitude = deg2rad($lat);
		$longitude = deg2rad($lon);
		
		$lat_t = asin(sin($latitude) / cos($berekenafstand));
		$lonPitago = asin(sin($berekenafstand) / cos($latitude));

		$top = rad2deg($latitude + $berekenafstand); // lat max
		$right = rad2deg($longitude + $lonPitago); // lon max
		$bottom = rad2deg($latitude - $berekenafstand); // lat min
		$left = rad2deg($longitude - $lonPitago); // lon min

		return array($top, $right, $bottom, $left);
	}
	
	/**
	 * Maak een database object en sla de data op
	 *
	 * @param object $dbo
	 * @return void
	 */
	public function __construct($dbo = NULL)
	{
		/*
		 * Roep de parent constructor aan om te controleren op
		 * een database object
		 */
		parent::__construct($dbo);
	}
}
?>

Re: Fatal error: Call to a member function bindParam() on a

Posted: Wed Jun 27, 2012 2:50 am
by social_experiment
Prepared statements (and PDO) isn't my forte but you would need a PDO object to use those statements; Do you create the object (PDO) in the parent class (connect) ?

Re: Fatal error: Call to a member function bindParam() on a

Posted: Wed Jun 27, 2012 3:04 am
by joepiooo1988

Code: Select all

<?php
class connect 
{

    /**
     * Opslaan van een database object
     * 
     * @var object Database object 
     */
    protected $db;
    
    /**
     * Controleren voor een db object of maak een db object
     * 
     * @param object $dbo een database object
     */
    protected function __construct($dbo = NULL)
    {
        if(is_object($dbo))
        {
            $this->db = $dbo;
        }
        else
        {
            // Constants zijn gevalideerd in config.inc.php
            $dsn = "mysql:host=".DBHOST.";dbname=".DB_NAME;
            try
            {
                $this->db = new PDO($dsn, DB_USER, DB_PASS);
            }
            catch (Exception $e)
            {
                die($e->getMessage());
            }
        }
    }
}

?>
Yes I do