4 Replies - 2324 Views - Last Post: 10 October 2012 - 01:56 AM Rate Topic: -----

#1 Joepiooo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-February 10

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

Posted 09 October 2012 - 07:39 AM

Can somebody help me to fix my script because I really dont know what is going wrong. It was working fine untill I added multiple languages. A colleague of mine wrote this script and I 'm lost atm because my php skills are way to poor for this.

This is the script for selecting and reading my tyres:

 <div id="bloklinks">
<?php
    include_once "class/tekst.class.php";
    $teksten = new tekst($dbo);
    $tekst = $teksten->getTeksten($website, $lang, $_GET['p']);
?>
                <h3><?php 
                    if(isset($_POST['searchtyre']) && $_POST['type'] != "type" || isset($_POST['searchtyre']) && $_POST['size'] != "size" || isset($_GET['type']) && isset($_GET['app']))
                    {
                        $searchinfo = new search($dbo);
                        $splittype = array("0","");
                        if(isset($_POST['type']) && $_POST['type'] != "type")
                        {
                            $splittype = split("-", $_POST['type']);
                        }
                        $type = isset($_GET['type']) ? $_GET['type'] : $splittype[0];
                        $splittype[1] = isset($splittype[1]) ? $splittype[1] : 0;
                        $app = isset($_GET['app']) ? $_GET['app'] : $splittype[1];
                        $size = NULL;
                        $typearray = $searchinfo->getTypes($website, $type);
                        $apparray = $searchinfo->getApplications($website, $type, $app);
                        echo "<span>";
                        if($type != 0){ echo " >> ".$typearray[0]->type_naam; }
                        if(isset($apparray[0])){ echo " >> ".$apparray[0]->application_naam; }
                        if(isset($_POST['size']) && $_POST['size'] != "size" || isset($_GET['size']) && $_GET['size'] != "size") 
                        { 
                            $size = isset($_GET['size']) ? $_GET['size'] : $_POST['size'];
                            echo " >> ".$size; 
                        } 
                        echo "</span>";
                    }
                    else
                    {
                        echo "Products"; 
                    } ?>&nbsp;</h3>
                <div class="hr"><hr/></div>
                <div id="bandentabel">
                    <table cellspacing="0">
                        <tr>
<?php
include_once "class/banden.class.php";
    $banden = new banden($dbo);
    if(isset($_POST['type']) && $_POST['type'] == "type" && $_POST['size'] == "size")
    {
        echo "\t\t\t\t\t\t\t<th colspan=\"2\" style=\"font-size: 12px; font-weight: normal;\">Please select a type & application or a size.</th>\n";
        $bandlijst = array();
    }
    else if(isset($type) && isset($app))
    {
        $app = ($app == 0) ? $app = NULL : $app = $app;
        $bandlijst = $banden->getBanden($website, NULL, $type, $app, $size);
        if(count($bandlijst) == 0)
        {
            echo "\t\t\t\t\t\t\t\t<th style=\"font-size: 12px; font-weight: normal;\">Company doesn't have a ".$typearray[0]->type_naam." ";
            if(isset($apparray[0])){ echo $apparray[0]->application_naam." "; }
            echo $size." inch tyre in her program, please select a different size.</th>\n";
        }
        $app = ($app == NULL) ? $app = 0 : $app = $app;
    }
    else
    {
        if(isset($size))
        {
            $bandlijst = $banden->getBanden($website, NULL, NULL, NULL, $size);
        }
        else
        {
            $bandlijst = $banden->getBanden($website);
        }
    }
    $counter = 0;
    $patternarray = array();
    foreach($bandlijst as $band)
    {
        if(!in_array($band->band_ID, $patternarray))
        {
            if($counter == 2)
            {
                echo "\t\t\t\t\t\t</tr>
\t\t\t\t\t\t<tr>\n";
                $counter = 0;
            }
            if(isset($type) && isset($app))
            {
                echo "\t\t\t\t\t\t\t<td><a href=\"?lang=".$_GET['lang']."&amp;p=2&amp;type=".$type."&amp;app=".$app;
                if(isset($size))
                {
                    echo "&amp;size=".$size;
                }
                echo "&amp;b=".$band->band_ID."#specification\"><img src=\"bestanden/banden/".$band->band_imgnaam."-K.png\" width=\"107\" height=\"23\" alt=\"".$band->band_pattern."\"><p>".$band->band_pattern."<br/><span>Specification</span></p></a></td>\n";
            }
            else if(isset($size))
            {
                echo "\t\t\t\t\t\t\t<td><a href=\"?lang=".$_GET['lang']."&amp;p=2&amp;type=0&amp;app=0&amp;size=".$size."&amp;b=".$band->band_ID."#specification\"><img src=\"bestanden/banden/".$band->band_imgnaam."-K.png\" width=\"107\" height=\"23\" alt=\"".$band->band_pattern."\"><p>".$band->band_pattern."<br/><span>Specification</span></p></a></td>\n";
            }
            else
            {   
                echo "\t\t\t\t\t\t\t<td><a href=\"?lang=".$_GET['lang']."&amp;p=2&amp;b=".$band->band_ID."#specification\"><img src=\"bestanden/banden/".$band->band_imgnaam."-K.png\" width=\"107\" height=\"23\" alt=\"".$band->band_pattern."\"><p>".$band->band_pattern."<br/><span>Specification</span></p></a></td>\n";
            }
            $counter++;
            array_push($patternarray, $band->band_ID);
        }
    }
    if($counter == 1)
    {
        echo "\t\t\t\t\t\t\t\t<th>&nbsp;</th>\n";
    }
?>
                        </tr>
                    </table>
                </div>
            </div>
<?php
    <div id="blokrechts">
                <h3>Tyre search</h3>
                <div class="hr"><hr/></div>
                <form action="?lang=<?php echo $_GET['lang'];?>&amp;p=2" method="post">
<?php
    $seltype = 0;
    $application = 0;
    $size = 0;
    $search = new search($dbo);
    if(isset($_POST['searchtyre']) && $_POST['type'] != "type" || isset($_POST['searchtyre']) && $_POST['size'] != "size" || isset($_GET['type']) && isset($_GET['app']))
    {
        $splittype = array("0","");
        if(isset($_POST['type']) && $_POST['type'] != "type") { $splittype = split("-", $_POST['type']); }
        $seltype = isset($_GET['type']) ? $_GET['type'] : $splittype[0];
        if($seltype == "type") { $seltype = 0; }
        $splittype[1] = isset($splittype[1]) ? $splittype[1] : 0;
        $application = isset($_GET['app']) ? $_GET['app'] : $splittype[1];
        $size = NULL;
        if(isset($_POST['size']) && $_POST['size'] != "size" || isset($_GET['size']) && $_GET['size'] != "size") { $size = isset($_GET['size']) ? $_GET['size'] : $_POST['size']; }
    }
?>
                    <table class="searchtable">
                        <tr>
                            <td>
                                <select name="type">
                                    <option value="type">-- Type &amp; Application --</option>
<?php
    $types = $search->getTypes($website);
    foreach($types as $type)
    {
        $selected = "";
        $applications = $search->getApplications($website, $type->type_ID);
        if(count($applications) > 0)
        {
            echo "\t\t\t\t\t\t\t\t\t<optgroup label=\"".$type->type_naam."\">\n";
            foreach($applications as $app)
            {
                $selected = "";
                if($application == $app->application_ID) { $selected = " selected=\"selected\""; }
                echo "\t\t\t\t\t\t\t\t\t\t<option value=\"".$type->type_ID."-".$app->application_ID."\"".$selected.">".$app->application_naam."</option>\n";
            }
            echo "\t\t\t\t\t\t\t\t\t</optgroup>\n";
        }
        else
        {
            if($type->type_ID == $seltype) { $selected = " selected=\"selected\""; }
            echo "\t\t\t\t\t\t\t\t\t<option value=\"".$type->type_ID."\"".$selected.">".$type->type_naam."</option>\n";
        }
    }               
?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select name="size">
                                    <option value="size">-- Size --</option>
<?php
    $inchlist = $search->getSizes($website);
    sort($inchlist);
    $doublelist = array();
    foreach($inchlist as $inch)
    {
        $selected = "";
        if(!in_array($inch->bandinfo_size, $doublelist))
        {
            if($size == $inch->bandinfo_size) { $selected = " selected=\"selected\""; }
            array_push($doublelist, $inch->bandinfo_size);
            echo "\t\t\t\t\t\t\t\t\t<option value=\"".$inch->bandinfo_size."\"".$selected.">".$inch->bandinfo_size."</option>\n";
        }
    }
?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type="submit" name="searchtyre" value="Search" />
                            </td>
                        </tr>
                    </table>
                </form>

                <h3>Contact us</h3>
                <div class="hr"><hr/></div>
                <table class="contacttable">
                    <tr>
                        <td class="itemtekst">Phone:</td>
                        <td>+31 12345678</td>
                    </tr>
                    <tr>
                        <td class="itemtekst">Fax:</td>
                        <td>+31 12345678</td>
                    </tr>
                    <tr>
                        <td class="itemtekst">E-mail:</td>
                        <td>info@email.com</td>
                    </tr>
                </table>
            </div>

?>
            <div class="clearboth">&nbsp;</div>
            <!--<a name="specification"></a>//-->
<?php
if(isset($_GET['b']))
{
    $headwaarde = $banden->getBanden($website, $_GET['b']);
?>
            <div id="matentabel">
                <table id="spectabel">
                    <tr>
                        <td rowspan="4" class="bandafb">
                            <a href="#" onclick="fx.changeImage('<?php echo $headwaarde->band_imgnaam; ?>', this); return false;">
                                <img src="afbeeldingen/vergroot.png" width="14" height="13" alt="vergroot/verklein" class="zoom"/>
                                <img src="bestanden/banden/<?php echo $headwaarde->band_imgnaam; ?>.png" width="206" height="260" alt="<?php echo $headwaarde->band_pattern; ?>"/>
                            </a>
                        </td>
                        <th><?php echo $headwaarde->band_pattern; ?></th>
                    </tr>
                    <tr>
                        <td style="padding-bottom: 10px;"><?php echo $headwaarde->band_text; ?></td>
                    </tr>
                    <tr>
                        <td><img src="afbeeldingen/specification.png" width="9" height="9" alt="specification"/>&nbsp;&nbsp;Specification</td>
                    </tr>
                    <tr>
                        <td>
                            <div>
                                <table class="spec">
                                    <tr>
<?php
    switch($headwaarde->band_type)
    {
        case "1":
        case "3":
        default:
?>
                                        <th>Item Code</th>
                                        <th>Tire Size</th>
                                        <th>Load Symbol</th>
                                        <th>Note</th>
                                        <th>Speed Max (KM/H)</th>
                                        <th>Overall Diameter (mm)</th>
                                        <th>Section Width (mm)</th>
                                        <th>Tread Depth (mm)</th>
                                        <th>Rim Width (inch)</th>
                                        <th>Weight (KGS)</th>
                                        <th>Single MAX load / pressure (KGS/PSI)</th>
                                        <th>Brandstof</th>
                                        <th>Geluid Db</th>
                                        <th>Geluid</th>
                                        <th>Grip</th>
<?php
            break;
        case "2":
?>
                                        <th>Item Code</th>
                                        <th>Tire Size</th>
                                        <th>Load Index (S/D)</th>
                                        <th>Ply Rating</th>
                                        <th>SI</th>
                                        <th>Overall Diameter (mm)</th>
                                        <th>Section Width (mm)</th>
                                        <th>Tread Depth (mm)</th>
                                        <th>Rim Width (inch)</th>
                                        <th>Weight (KGS)</th>
                                        <th>Single MAX load / pressure (KGS/PSI)</th>
                                        <th>Dual MAX load / pressure (KGS/PSI)</th>
                                        <th>Brandstof</th>
                                        <th>Geluid Db</th>
                                        <th>Geluid</th>
                                        <th>Grip</th>
<?php
            break;
    }
?>
                                    </tr><?php

    $bandinfo = $banden->getBandenInfo($website, $headwaarde->band_ID);

    foreach($bandinfo as $banditem)
    {
        switch($headwaarde->band_type)
        {
            case "1":
            case "3":
            default:
                echo "\n\t\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_itemcode."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_size."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_loadsymbol."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_note."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_speedmax."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_overalldiameter."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_sectionwidth."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_treaddepth."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_rim."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_weight."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_singlemax."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_fuel."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_geluid_db."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_geluid_img."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_grip."</td>
\t\t\t\t\t\t\t\t\t</tr>";
                break;
            case "2":
                echo "\n\t\t\t\t\t\t\t\t\t<tr>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_itemcode."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_size."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_loadindex."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_plyrating."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_speedsymbol."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_overalldiameter."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_sectionwidth."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_treaddepth."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_rim."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_weight."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_singlemax."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_dualmax."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_fuel."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_geluid_db."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_geluid_img."</td>
\t\t\t\t\t\t\t\t\t\t<td>".$banditem->bandinfo_grip."</td>
\t\t\t\t\t\t\t\t\t</tr>";
                break;
        }
    }
    echo "\n";
?>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
<?php
}
?>



This is my Class to connect to my database:

<?php
include_once "connect.class.php";

class banden extends connect
{   

    private $bandenlijst;

    private $bandinfolijst;

    public function getBanden($database, $pattern = NULL, $type = NULL, $app = NULL, $size = NULL)
    {
        $sql = "SELECT * FROM `".$database."_banden`";

        if(!empty($pattern))
        {
            $sql .= " WHERE band_ID=:pattern LIMIT 1";
        }
        else if(!empty($type))
        {
            if(!empty($size))
            {
                $sql .= " INNER JOIN _bandinfo ON bandinfo_pattern=band_ID";
            }
            $sql .= " WHERE band_type LIKE :type";
            if(!empty($app))
            {
                $sql .= " AND band_application LIKE :app";
            }
            if(!empty($size))
            {
                $sql .= " AND bandinfo_size=:size";
            }
        }
        else if(!empty($size))
        {
            $sql .= " INNER JOIN ".$database."_bandinfo ON bandinfo_pattern=band_ID WHERE bandinfo_size=:size";
        }
        else
        {
            $sql .= " ORDER BY band_ID DESC";
        }
        try
        {
            $stmt = $this->db->prepare($sql);
            if(!empty($pattern))
            {
                $stmt->bindParam(":pattern", $pattern, PDO::PARAM_STR);
            }
            if(!empty($type))
            {
                $type = $type;
                $stmt->bindParam(":type", $type, PDO::PARAM_INT);
                if(!empty($app))
                {
                    $app = $app;
                    $stmt->bindParam(":app", $app, PDO::PARAM_STR);
                }
            }
            if(!empty($size))
            {
                $stmt->bindParam(":size", $size, PDO::PARAM_STR);
            }
            $stmt->execute(); 
            $this->bandenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();

            if(!empty($pattern) && isset($this->bandenlijst[0]))
            {
                $this->bandenlijst = $this->bandenlijst[0];
            }

            return $this->bandenlijst;
        }
        catch (Exception $e)
        {
            die ($e->getMessage());
        }
    }

public function getBandeninfo($database, $pattern=NULL, $id = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM ".$database."_bandeninfo";
        if(!empty($id))
        {
            if(is_numeric($id))
            {
                $sql .= " WHERE bandinfo_ID=:id LIMIT 1";
            }
            else
            {
                return NULL;
            }
        }
        else if(!empty($pattern) && is_numeric($pattern))
        {
            $sql .= " WHERE bandinfo_pattern=:pattern";
        }
        if(empty($id) && !empty($limit))
        {
            if($limit == 1) { $limit = 0; }
            $sql .= " LIMIT ".$limit.", 15";
        }
        try
        {
            $stmt = $this->db->prepare($sql);
            if(!empty($id) && is_numeric($id))
            { 
                $stmt->bindParam(":id", $id, PDO::PARAM_INT); 
            }
            else if(!empty($pattern) && is_numeric($pattern))
            {
                $stmt->bindParam(":pattern", $pattern, PDO::PARAM_INT);
            }
            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();

            return $result;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }

    public function __construct($dbo = NULL)
    {

        parent::__construct($dbo);
    }
}
?>



What is going terrible wrong is the following:

When I select something in my first drop down en submit it everything is fine...

But when I add a size to it I get this error:

Fatal error: Call to a member function bindParam() on a non-object in /website.com/class/banden.class.php on line 53

Line 53 =
$stmt->bindParam(":type", $type, PDO::PARAM_INT);


When I only select a size and no type I get this error (almost the same):

Fatal error: Call to a member function bindParam() on a non-object in /website.com/class/banden.class.php on line 62

Line 62 =
$stmt->bindParam(":size", $size, PDO::PARAM_STR);


Can somebody help me? I dont see myself fixing it on my own:(

Thank you very much

Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error: Call to a member function bindParam() on a non-object in

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

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

Posted 09 October 2012 - 07:48 AM

var_dump($stmt);

Is it a PDO Statement object? To find out what's wrong you could either enable error exceptions in PDO or use errorInfo() off the DB object as Atli says down there \/

$info = $this->db->errorInfo();

This post has been edited by RudiVisser: 09 October 2012 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 unum_sanctum()  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-October 12

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

Posted 09 October 2012 - 08:17 AM

You'll want to check if $stmt is an object after you attempt the db->prepare() function call.

$stmt = $this->db->prepare($sql);
if( is_object($stmt) )
{
    //Alternatively you can opt to return an error rather than throw an exception
    // but you already have a catch statement so might as well make use of it
    throw new Exception("Prepare returned a non-object: " . var_export($stmt,1) );
}



OOPS. The code is supposed to be this:
$stmt = $this->db->prepare($sql);
if( is_object($stmt) == false) //this is the change
{
    //Alternatively you can opt to return an error rather than throw an exception
    // but you already have a catch statement so might as well make use of it
    throw new Exception("Prepare returned a non-object: " . var_export($stmt,1) );
}



Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

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

Posted 09 October 2012 - 09:48 AM

The PDO::prepare() function returns a boolean FALSE when it fails, so you can also just check if it's false. It's probably a CPU cycle or two faster. (Not that that is a great concern in most cases.)

$stmt = $this->db->prepare($sql);
if(!$stmt)
{
    $info = $this->db->errorInfo();
    throw new Exception("Failed to prepare query: " . $info[2]);
}


Was This Post Helpful? 1
  • +
  • -

#5 Joepiooo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-February 10

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

Posted 10 October 2012 - 01:56 AM

You guys are great! I need to do more with displaying errorInfo...

That really helpt me fixing this! It was a simple . in connecting to my database....

Again, thank you... I was almost desperate;)

You guys made my day:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1