Fatal error: Call to a member function bind_param() on a non
Posted: Thu Feb 19, 2009 5:38 pm
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:
Relevant database table was built using the following mysql code:
This is the output I receive when I try submit the form that calls books.php:
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!
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();
?>
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';
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