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

i need some help. i dont understand how to fix this error

Page 1 of 1

14 Replies - 37485 Views - Last Post: 27 March 2007 - 11:25 PM Rate Topic: -----

#1 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 24 March 2007 - 01:51 PM

here is the code i'm useing
   define('PUN_ROOT', './');
   require PUN_ROOT.'include/common.php';
   		 
   if(!$pun_user['is_guest'])
   {
	   $aboutme = $_POST['about_me'];
	   $interests = $_POST['interests'];
	   	  
	   	$query1 = $dbh->prepare('UPDATE users SET fm_aboutme=? WHERE id=?');// or error('Unable to update about me', __FILE__, __LINE__, $db->error());
		  $query2 = $dbh->prepare('UPDATE '.$db->prefix.'users SET fm_interests=? WHERE id=?') or error('Unable to update interests', __FILE__, __LINE__, $db->error());
			 
		   $query1->execute(array($db->prefix,$aboutme, $pun_user['id']));
		   $query2->execute(array($interests, $pun_user['id'])); 	
	   
			 
	   redirect('field.php?id='.$pun_user['id']);




its giving me the error on the topic title. and i need to know how to fix it. also i am useing PunBB with this. anyway i tried turning $dbh into $db, and making a new dbh, but nothing worked.

This post has been edited by SoraCross: 24 March 2007 - 02:50 PM


Is This A Good Question/Topic? 0
  • +

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

#2 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

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

Posted 24 March 2007 - 02:14 PM

For starters, there's an extra semi-colon floating right under the start of your if statement.

Then, I'm not sure if it's just for displaying it here or if a certain database allows them, but there's question marks in both of your queries. Is that correct syntax for your database?

err, and for $query1 and $query2, $dbh, at least for this example, doesn't seem to be an object.

This post has been edited by Styx: 24 March 2007 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 24 March 2007 - 02:19 PM

well i dont see the extra semi-colon, and thats how you do it with the prepare function. doing it like that makes it hard to SQL inject. those ?'s become variables when the query is executed.
Was This Post Helpful? 0
  • +
  • -

#4 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

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

Posted 24 March 2007 - 02:24 PM

They you're blind because it's right there under the $pun_user.

As for the error, what is it?
Was This Post Helpful? 0
  • +
  • -

#5 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 24 March 2007 - 02:42 PM

ummm yea, i still dont see that o.o;;;


and as i said the error is: Call to a member function prepare() on a non-object
Was This Post Helpful? 0
  • +
  • -

#6 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

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

Posted 24 March 2007 - 02:47 PM

*slams head on desk*

if(!$pun_user['is_guest'])
   {
IT'S RIGHT HERE! ->; <- IT'S RIGHT HERE!
	   $aboutme = $_POST['about_me'];



Sorry about missing the error. Well it would appear that $dbh hasn't been assigned in any files that have been included.
Was This Post Helpful? 0
  • +
  • -

#7 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 24 March 2007 - 02:51 PM

lol my bad. i dont know where that came from, but that ; wasnt in the actual code, so thats not what was causeing the error.

edit: how do i assign $dbh? sorry i'm still new with PHP

This post has been edited by SoraCross: 24 March 2007 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#8 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

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

Posted 24 March 2007 - 03:01 PM

dbh probably stands for "database handle". I'm not that familiar with PunBB, but there is probably a file that sets those variables up. Take a look at index.php and have a look at what files are included and use those as a starting point.
Was This Post Helpful? 0
  • +
  • -

#9 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 25 March 2007 - 11:27 AM

umm, okay i found this file from doing what you said. so is $db the database handler? i already tried running the code at the top of this page with $db instead of $dbh, but it said it couldnt find prepare();

<?php
/***********************************************************************

  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)

  This file is part of PunBB.

  PunBB is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PunBB is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/


// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
	exit;


//
// Return current timestamp (with microseconds) as a float (used in dblayer)
//
if (defined('PUN_SHOW_QUERIES'))
{
	function get_microtime()
	{
		list($usec, $sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}
}


// Load the appropriate DB layer class
switch ($db_type)
{
	case 'mysql':
		require PUN_ROOT.'include/dblayer/mysql.php';
		break;

	case 'mysqli':
		require PUN_ROOT.'include/dblayer/mysqli.php';
		break;

	case 'pgsql':
		require PUN_ROOT.'include/dblayer/pgsql.php';
		break;

	case 'sqlite':
		require PUN_ROOT.'include/dblayer/sqlite.php';
		break;

	default:
		error('\''.$db_type.'\' is not a valid database type. Please check settings in config.php.', __FILE__, __LINE__);
		break;
}


// Create the database adapter object (and open/connect to/select db)
$db = new DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);


Was This Post Helpful? 0
  • +
  • -

#10 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

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

Posted 25 March 2007 - 05:15 PM

Are you doing that before or after you include the file that defines $db?
Was This Post Helpful? 0
  • +
  • -

#11 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 25 March 2007 - 06:18 PM

after, i include that file at like the top of my code on all my files.

EDIT: okay, i managed to get a bit farther, but now i got this strange error

$dbh = new PDO('mysql: host='.$db_host.'; dbname='.$db_name.'',$db_username, $db_password); 



by adding that line to the db handleing file i get this error


Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' in /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php:71 Stack trace: #0 /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php(71): PDO->__construct('mysql: host=db....', 'username', 'password') #1 /home/.greasy/soracross/soracross.com/forums/include/common.php(90): require('/home/.greasy/s...') #2 /home/.greasy/soracross/soracross.com/forums/index.php(27): require('/home/.greasy/s...') #3 {main} thrown in /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php on line 71



and my code is the same as the code they say to use here: http://us2.php.net/m.../en/ref.pdo.php

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);


This post has been edited by SoraCross: 25 March 2007 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 27 March 2007 - 01:43 PM

cant anyone help?
Was This Post Helpful? 0
  • +
  • -

#13 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

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

Posted 27 March 2007 - 07:22 PM

For the first argument of PDO, you open with a single quotation mark, but at the end, you add a catenation mark (period) and then a double quotation mark.

It should be:
$dbh = new PDO("mysql: host=$db_host; dbname=$db_name",$db_username, $db_password);

This post has been edited by Styx: 27 March 2007 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#14 SoraCross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 07

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

Posted 27 March 2007 - 07:34 PM

okay when i use that code i get
Parse error: syntax error, unexpected $end in common_db.php on line 73



bcause it never closes the string, but when i change '' (two single quotes) to " (a double quote) which fixes that problem i get the same error i had before

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' in /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php:71 Stack trace: #0 /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php(71): PDO->__construct('mysql: host=db....', 'username', 'password') #1 /home/.greasy/soracross/soracross.com/forums/include/common.php(90): require('/home/.greasy/s...') #2 /home/.greasy/soracross/soracross.com/forums/index.php(27): require('/home/.greasy/s...') #3 {main} thrown in /home/.greasy/soracross/soracross.com/forums/include/dblayer/common_db.php on line 71


Was This Post Helpful? 0
  • +
  • -

#15 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

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

Posted 27 March 2007 - 11:25 PM

yeah, I caught that about a minute after I posted :P

sorry I can't be much help here, there might be something wrong with your mysql socket setup, see if this thread or this thread helps any, since they seemed to have a similar problem x_x
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1