6 Replies - 541 Views - Last Post: 11 September 2012 - 05:36 AM Rate Topic: -----

#1 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Fatal PHP PDO Error

Posted 10 September 2012 - 12:12 PM

Alright, so I'm just working on a simple little update script using the new PDO (of which, I am a first-time user) and when testing my script, I get an error saying Fatal error: Call to a member function prepare() on a non-object, and the script is pasted below.

Le Code:
<?PHP
	$name = $_POST['name'];
	$passcode = $_POST['passcode']; //Don't comment on this, I'll use it after this error is fixed...
	$news = $_POST['news'];
					
	try {
		//Connect to the database
		include 'dbconnect.php';
		
		$updatenews = $pdo->prepare('UPDATE news SET name = :name, news = :news WHERE id = :id');
		$updatenews->execute(array(':id' => 1, ':name' => $name, ':news' => $news));
	} catch(PDOException $e) {
		echo "<p>Sorry, there was an error;   " . $e->getMessage() . ".</p>"; //Print out the exception
	}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Fatal PHP PDO Error

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,974
  • Joined: 08-June 10

Re: Fatal PHP PDO Error

Posted 10 September 2012 - 12:16 PM

It's telling you that your $pdo variable is not an object, and thus calling prepare() on it will not work. Presumably the PDO connection code in your 'dbconnect.php' file is failing somehow. We'd need to see that to know more.
Was This Post Helpful? 0
  • +
  • -

#3 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Fatal PHP PDO Error

Posted 10 September 2012 - 12:20 PM

Well, here's 'dbconnect.php', and I'm thinking of removing the try-catch block from it.

Le Code:
<?PHP
$username = user;
$password = password;

try {
		$conn = new PDO('mysql: host=localhost; dbname=db', $username, $password);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch(PDOException $e) {
		echo "<p>Sorry, there was an error;   " . $e->getMessage() . ".</p>";
	}
?>


This post has been edited by skymonkier: 10 September 2012 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6049
  • View blog
  • Posts: 23,479
  • Joined: 23-August 08

Re: Fatal PHP PDO Error

Posted 10 September 2012 - 12:28 PM

So, is the object named $pdo, or is it named $conn?
Was This Post Helpful? 0
  • +
  • -

#5 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Fatal PHP PDO Error

Posted 10 September 2012 - 12:30 PM

...

:oops:

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,137
  • Joined: 08-June 10

Re: Fatal PHP PDO Error

Posted 10 September 2012 - 10:29 PM

View Postskymonkier, on 10 September 2012 - 09:20 PM, said:

<?PHP
$username = user;
$password = password;

try {
		$conn = new PDO('mysql: host=localhost; dbname=db', $username, $password);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch(PDOException $e) {
		echo "<p>Sorry, there was an error;   " . $e->getMessage() . ".</p>";
	}
?>


two notes:
- unless user & password are actual constants (in which case they should be written upper-case), strings are delimited by quotes. if you indeed use constants, put them into the PDO constructor directly.
- remove that try...catch block. exceptions are meant to be caught where you can handle the problem (which is the try...catch block of the other script (where you can skip all the other DB code))
Was This Post Helpful? 0
  • +
  • -

#7 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Re: Fatal PHP PDO Error

Posted 11 September 2012 - 05:36 AM

I thought about removing the try-catch block from it, but I wasn't sure. I'll also fix user/pass. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1