I've been using PHP for a while, but I'm just starting to try PDO. I'm trying to create a handle in an include file that connects to the database, but I get this error:
[COLOR="Red"][B]( ! ) Fatal error: Call to a member function query() on a non-object in /Users/peterv/Sites/lp2/INCLUDES/process_books.php on line 31[/B][/COLOR]
process_switch.php: This calls the pdo_dbc function in the dbcp.php file, then calls the process_books.php script that incurs the error:
<?php
// Updated: Thursday March 24, 2011 - 1:40 PM
//Connect to PROJECT1 database.
[B][COLOR="Red"] require_once('./INCLUDES/dbcp.php');
$dbh = pdo_dbc();[/COLOR][/B]
//
// Determine processing by screenid.
$screenid = $_POST['screenid'];
// $formoption: search, add, etc.
$formoption=$_POST['formoption'];
$title=$_POST['title'];
//
switch ($screenid) {
case 'authors_form1.php':
include('./INCLUDES/process_authors.php');
break;
case 'books_form1.php':
[B][COLOR="Red"] include('./INCLUDES/process_books.php');[/COLOR][/B]
break;
default:
$display_block = "Error - no option selected.";
}
?>
dbcp.php:
<?php
// Updated: Thursday March 24, 2011 - 1:40 PM
function pdo_dbc()
{
$dsn = "mysql:host=localhost;dbname=PROJECT1";
$user = "tester1";
$password = "testuser";
//
try {
[COLOR="Red"][B]$dbh[/B][/COLOR] = new PDO($dsn, $user, $password);
[COLOR="Red"][B]$dbh[/B][/COLOR]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return ([COLOR="Red"][B]$dbh[/B][/COLOR]);
} catch (Exception $e) {
$emsg = $e->getMessage();
include('./INCLUDES/dbcp_error_msg.php');
}
}
?>
The calling script: process_books.php
***
function search($title, $bookid) {
echo "Entering Search Function...";
//*****************************************************************************/
//* S E A R C H */
//*****************************************************************************/
if (($_POST['bookid']) != NULL) {
$record_key = $_POST['bookid'];
$field = 'bookid';
}else{
$field = 'title';
$record_key = $_POST['title'];
}
[COLOR="Blue"][B]The next line is the one that is generating the error message:[/B][/COLOR]
[COLOR="Red"][B] $sth = $dbh->query("SELECT * FROM MYBOOKS where $field like '%$record_key%'");[/B][/COLOR]
$query = "SELECT * FROM MYBOOKS where $field like '%$record_key%'";
$result = mysql_query($query);
$rows=mysql_affected_rows(); // Number of rows returned by the query.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
} else {
if ($rows > 0) {
while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC)) {
$authorid = $_POST['authorid'];
$full_name = $_POST['full_name'];
$bookid = $_POST['bookid'];
$authorid2 = $_POST['authorid'];
$title = $_POST['title'];
$isbn = $_POST['isbn'];
$type = $_POST['type'];
$cover_type = $_POST['cover_type'];
$pages = $_POST['pages'];
$copyright = $_POST['copyright'];
$date_finished = $_POST['date_finished'];
$has_been_read = $_POST['has_been_read'];
$needed = $_POST['needed'];
return array ($rows, $bookid, $authorid2, $title, $isbn, $type,
$cover_type, $pages, $copyright, $date_finished,
$has_been_read, $needed, $fname, $lname);
}
}
}
}
[B][COLOR="Blue"]The code below calls the search function listed above:[/COLOR][/B]
***
//*****************************************************************************/
//* S E A R C H B Y T I T L E */
//*****************************************************************************/
case 'Search':
echo "Entering Search...";
list ($rows, $bookid, $authorid2, $title, $isbn, $type, $cover_type,
$pages, $copyright, $date_finished, $has_been_read, $needed,
$full_name) = [B][COLOR="Red"]search($title,$bookid)[/COLOR][/B];
if (is_null($rows)) { // If no rows were returned from query.
$display_block = "No books were found.";
}
[COLOR="Green"][B][I]I know the code below this won't work with PDO, but the error is prior to this code, so I will
modify it after I fix the "non-object" error above! [/I][/B][/COLOR]
$query = "SELECT * FROM myauthors WHERE authorid = '$authorid2'";
$result = mysql_query($query);
$rows=mysql_affected_rows();
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
} else {
if ($rows < 1) {
// echo "NO AUTHORS RETURNED<br>";
}else{
while ($_POST = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $_POST['id'];
$authorid = $_POST['authorid'];
$full_name = $_POST['full_name'];
}
}
}
break;
Any suggestions or help will be greatly appreciated!