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

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1151 Views - Last Post: 11 January 2014 - 06:01 PM Rate Topic: -----

#1 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 07:16 AM

I am getting this ERROR!!! SO ANNOYING!!

$query = "SELECT `DVD ID`, `Name Of the DVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();


Rest of the code is below

<?php

session_start();

?>


<!DOCTYPE HTML>


<html>

  <head>
  
      <title>Shopping Cart</title>
        <link type="text/css" rel="stylesheet" href="basket.css" />
  
  </head>
  <body>
    <?php
	  $action = isset($_GET['action']) ? $_GET['action'] : "";
      $name = isset($_GET['Name Of the DVD']) ? $_GET['Name Of the DVD'] : "";
	  
	  if($action=='add'){
       echo "<div>" . $name . " was added to your cart.</div>";
     }

      if($action=='exists'){
       echo "<div>" . $name . " already exists in your cart.</div>";
     }

     require "connect.php";
  
     $query = "SELECT `DVD ID`, `Name Of the DVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();
	 
	 $num = $stmt->rowCount();
	 
	 if($num>0){
      echo "<table border='0'>";//start table
    
        // our table heading
            echo "<tr>";
            echo "<th class='textAlignLeft'>DVD Name</th>";
            echo "<th>Quantity</th>";
            echo "<th>Action</th>";
            echo "</tr>";
        
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            
            //creating new table row per record
            echo "<tr>";
                echo "<td>{$name}</td>";
                echo "<td class='textAlignRight'>{$price}</td>";
                echo "<td class='textAlignCenter'>";
                    echo "<a href='AddToCart.php?id={$id}&name={$name}' class='customButton'>";
                        echo "<img src='images/add-to-cart.png' title='Add To Cart' />";
                    echo "</a>";
                echo "</td>";
            echo "</tr>";
        }
        
    echo "</table>";
}

// no products in the database
else{
    echo "No products found.";
}

?>
  </body>

</html>


Thanks for any help

Is This A Good Question/Topic? 0
  • +

Replies To: Call to a member function prepare() on a non-object

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 07:28 AM

Where is $dbhandle defined and instantiated?

Why have you decided to put spaces in your field-names? You didn't have them before. This is incompatible with form-control names and much more trouble than it is worth for field-names.
Was This Post Helpful? 0
  • +
  • -

#3 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 08:06 AM

Hello again, 4dbhandle is in a file called connect.php and this is a different table
Was This Post Helpful? 0
  • +
  • -

#4 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 08:11 AM

Should I put the connect to the database code in the same file?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 08:17 AM

You would typically include, or require, connect.php so that the object $dbhandle is available to you in this file.




This may be for a separate table but I still wouldn't use spaces in field-names, and they are not valid in form-control names. In any case, different tables can have the same field-names.
Was This Post Helpful? 0
  • +
  • -

#6 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 08:23 AM

Yh, I tried a require statement and it still did not work, then I tried put the code into the same file and it still did not work. Its giving me the same error.
Was This Post Helpful? 0
  • +
  • -

#7 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 08:29 AM

I have changed the field names as well, its still not working
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 08:30 AM

What is the code in connect.php?
Was This Post Helpful? 0
  • +
  • -

#9 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 08:32 AM

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

$hostname = "localhost"; 
$username = "1104107";
$password = "r940c1";
$database = "db1104107";
//echo '24';

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password);
$db = mysql_select_db($database, $dbhandle)
  or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";

?>

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,142
  • Joined: 12-June 08

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

Posted 10 January 2014 - 08:42 AM

Moved to PHP from general web dev
Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 08:59 AM

Your connect.php uses the procedural/functional version of the mysql extension, $dbhandle->.. is attempting to use the Class/Object based approach. The two are incompatible and you need to stick to one or the other.

In fact, the Class-based approach requires the mysqli or PDO extension library, not mysql.
Was This Post Helpful? 0
  • +
  • -

#12 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 09:08 AM

How do I do it in a procedural way?

I just looked it at the PDO way and it looks complicated. I rather stick to the produral way

Especially for the moment. Maybe later I will switch to MYSQLI and PDO statements
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 09:09 AM

The procedural mysql extension is deprecated and "not recommended for writing new code". As you don't know how to write it procedurally anyway, you might as well learn how to do it with PDO.

Otherwise, read the link for the mysql library, especially the comments and examples.

This post has been edited by andrewsw: 10 January 2014 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

#14 mfredy92  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 09-May 13

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

Posted 10 January 2014 - 09:18 AM

I tried using the PDO. Can you check if I have not missed anything? Thanks


<?php

session_start();

?>
<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

$hostname = "localhost"; 
$username = "1104107";
$password = "r940c1";
$database = "db1104107";
//echo '24';

//connection to the database
try {
    $dbhandle = new PDO("mysql:host={$hostname};database={$database}", $username, $password);
}

//to handle connection error
catch(PDOException $exception){
    echo "Connection error: " . $exception->getMessage();
}

?>

<!DOCTYPE HTML>


<html>

  <head>
  
      <title>Shopping Cart</title>
        <link type="text/css" rel="stylesheet" href="basket.css" />
  
  </head>
  <body>
    <?php
	  $action = isset($_GET['action']) ? $_GET['action'] : "";
      $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
	  
	  if($action=='add'){
       echo "<div>" . $name . " was added to your cart.</div>";
     }

      if($action=='exists'){
       echo "<div>" . $name . " already exists in your cart.</div>";
     }

     require "connect.php";
  
     $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();
	 
	 $num = $stmt->rowCount();
	 
	 if($num>0){
      echo "<table border='0'>";//start table
    
        // our table heading
            echo "<tr>";
            echo "<th class='textAlignLeft'>DVD Name</th>";
            echo "<th>Quantity</th>";
            echo "<th>Action</th>";
            echo "</tr>";
        
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            
            //creating new table row per record
            echo "<tr>";
                echo "<td>{$name}</td>";
                echo "<td class='textAlignRight'>{$Quantity}</td>";
                echo "<td class='textAlignCenter'>";
                    echo "<a href='AddToCart.php?id={$id}&name={$name}' class='customButton'>";
                        echo "<img src='images/add-to-cart.png' title='Add To Cart' />";
                    echo "</a>";
                echo "</td>";
            echo "</tr>";
        }
        
    echo "</table>";
}

// no products in the database
else{
    echo "No products found.";
}

?>
  </body>

</html>

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

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

Posted 10 January 2014 - 10:08 AM

Does it work? That is the easiest way to tell.

I assume you no longer need "connect.php".

I would get rid of the redundant php-tags at the top and put session_start() as the very first line.

There shouldn't be a blank line before the DOCTYPE either.

This post has been edited by andrewsw: 10 January 2014 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3