0

When i run my code i receive the following error:

Fatal error: Call to a member function fetch() on a non-object in C:\Program Files\EasyPHP-5.3.8.0\www\Practice\staff.php on line 20

The code that causes the error is:

<?php
	 require_once "db_connect.php";
	 $dbh = db_connect ();

	 $dbh->exec ("CREATE TABLE Staff(
					UserId int NOT NULL AUTO_INCREMENT,
					PRIMARY KEY(UserId),
					Username varchar(65) NOT NULL,
					Password varchar(65) NOT NULL
					)");

	 $dbh->exec ("INSERT INTO Staff 
				 (UserId, Username, Password)
				  VALUES (0000, 'Ben', 'Password')");
	 
	 $sql = "SELECT * FROM Staff";
	 $stmt = $dbh->query($sql);
	
	 $result = $stmt->fetch(PDO::FETCH_ASSOC);
	 
	 foreach($result as $key=>$val){
		  echo "$val";
	 }
  
?>

The files also included are:

db_connect.php

<?php

function db_connect ()
  {
  include'db_connection.inc';
  $dbh = new PDO("mysql:host=127.0.0.1;$database", $user, $password);
  return ($dbh);
  }
?>

and :

db_connection.inc

<?php
$host = 'mysql:host=127.0.0.1;';
$user = 'root';
$password = 'mysql';
$database = 'library_db';
?>

I know the error states what is wrong but i am not sure which PDO fetch command i should be using or if there is anything else i am doing wrong.

What i want for it to do is print out the values of the record in the Staff table.

Any help is appreciated, thanks.

Edited by BenzZz: n/a

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by Stefano Mtangoo
0

Did you include db_connection.inc file into db_connect.inc ? Perhaps, like below:

<?php
include('db_connection.inc');

function db_connect ()
  {
  include'db_connection.inc';
  $dbh = new PDO("mysql:host=127.0.0.1;$database", $user, $password);
  return ($dbh);
  }
?>
0

have you checked $stmt = $dbh->query($sql); does not return false?
try something like this

$stmt = $dbh->query($sql);
if(!$stmt){
die("PDO Statement is false!");
}else{
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//work with your resultset here
}

PDO::query() returns a PDOStatement object, or FALSE on failure.

Edited by Stefano Mtangoo: n/a

0

have you checked $stmt = $dbh->query($sql); does not return false?
try something like this

Just tried that code and it has returned that the PDO statement is false so the query isn't working, any ideas why?

Is it that the query is wrong or could the db connection not be working?

Thanks for the help.

0

Just tried that code and it has returned that the PDO statement is false so the query isn't working, any ideas why?

Is it that the query is wrong or could the db connection not be working?

Thanks for the help.

From PHP Manual
PDO::errorInfo() returns an array of error information about the last operation performed by this database handle. The array consists of the following fields:

So do

foreach($dbh->errorInfo() as $key=>$info){
echo "error: ".$key." ".$info."<br />";
}
0

Opened up mysql in cmd and noticed that the Staff table wasn't being created and data was not being inserted.

I checked the pdo connection to the database and realised i had missed db_name= out before the database name 'library_db' which is what the cause was xD

Thanks for all of the help :)

0

Opened up mysql in cmd and noticed that the Staff table wasn't being created and data was not being inserted.

I checked the pdo connection to the database and realised i had missed db_name= out before the database name 'library_db' which is what the cause was xD

Thanks for all of the help :)

Mark it solved then!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.