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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
samcrockett
Forum Newbie
Posts: 2
Joined: Thu Feb 19, 2009 5:25 pm

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

Post by samcrockett »

I learning to build prepared statements using the mysqli class in php to connect to databases. Below is the code relevant to a little script a wrote to add books to a database.

It works by filling out a form that is then posted to a php script called books.php. The script takes care of connecting to the database and sending queries.

Code for connecting/query datatbase:
 

Code: Select all

<?php
/* Connect to database and add book information */
 
/* Book variables */
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$description = trim($_POST['description']);
$image = trim($_POST['image']);
$amazon = trim($_POST['amazon']);
 
/* Database login information */
require_once 'includes/constants.php';
 
/* Create new connection to database */
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
 
if (mysqli_connect_errno()) {
    printf("Can't connect to MySQL Server. Errorcode: %s\n",
    mysqli_connect_error());
    exit;
}
 
/* Create prepared statement query */
$stmt = $mysqli->prepare("INSERT INTO books('title', 'author', 'description', 'image', 'amazon') VALUES(?, ?, ?, ?, ?)");
 
$stmt->bind_param('sssss', $title, $author, $description, $image, $amazon);
 
 
/* Execute query */
$stmt->execute();
 
printf("%d Book added.\n", $stmt->affected_rows);
 
/* Close statement and connection */
$stmt->close();
 
/* Close connection */
$mysqli->close();
    
?>
 
Relevant database table was built using the following mysql code:

Code: Select all

 
--
-- Table structure for table `books`
--
 
CREATE TABLE IF NOT EXISTS `books` (
  `title` varchar(32) NOT NULL COMMENT 'title of book',
  `author` varchar(64) NOT NULL COMMENT 'author of book',
  `description` text NOT NULL COMMENT 'description of book',
  `image` varchar(128) NOT NULL COMMENT 'link to image on server',
  `amazon` varchar(256) NOT NULL COMMENT 'amazon affiliate link',
  PRIMARY KEY  (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='information about books listed on site';
 
This is the output I receive when I try submit the form that calls books.php:

Code: Select all

 
Fatal error: Call to a member function bind_param() on a non-object in /path/path/path/books.php on line 26
 
I'm still new to using prepared statements and the mysqli class to work with databases so any help with this would be greatly appreciated. Thanks!
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

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

Post by Reviresco »

bind_param() needs to be called with the name of the class object (not sure what that is here, nor the name of the class). For example, if the class is called "mysqli" and the class object was declared:

Code: Select all

$some_name = new mysqli;
You would use:

Code: Select all

$some_name->bind_param('sssss', $title, $author, $description, $image, $amazon);
samcrockett
Forum Newbie
Posts: 2
Joined: Thu Feb 19, 2009 5:25 pm

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

Post by samcrockett »

Thanks for your clarification. I'm still trying to get the hang of using mysqli and php in an OOP way. At this point in time I don't have any custom classes in this little php script. Maybe it would help if I gave you an idea of what I based this off of.

This is code from a tutorial explaining how to use the mysqli class:

Code: Select all

 <?php
$mysqli = new mysqli('localhost', 'user', 'password', 'world');
 
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
 
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
 
/* execute prepared statement */
$stmt->execute();
 
printf("%d Row inserted.\n", $stmt->affected_rows);
 
/* close statement and connection */
$stmt->close();
 
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", $mysqli->affected_rows);
 
/* close connection */
$mysqli->close();
?>
My little app is logically the same as this example, but I still must being doing something wrong with the prepared statements. I don't think it is the msql query itself because it seems to run fine when I run it through the command line.
Post Reply