Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xam:6 Stack trace: #0 {main} thrown in C:

can someone tell me what the below error is all about? The following code is a form for
a PDO update:


PHP code:

<?php
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
require "getprerentdb.php"; // Database connection details. 

$id=2; // member id is fixed here 
$count=$dbo->prepare("select * from certtbl WHERE id=:id");
$count->bindParam(":id",$id,PDO::PARAM_INT,1);

if($count->execute()){
echo " Success <br>";
$row = $count->fetch(PDO::FETCH_OBJ);
}else{
print_r($dbo->errorInfo());
}
/// Display the form to collect fresh data /// 
echo "<form name='myForm' action='certification-update.php' method=post>
<input type=hidden name=id value='$id'>
<table class='t1'> <input type=hidden name=todo value='change-data'>
<tr><th colspan=2>Update Profile $row->userid</th></tr>
<tr class='r10'><td>Unit</td><td><input type=text name='unit' value='$row->unit'></td></tr>
<tr class='r9'><td>Tenant</td><td><input type=text name='tenant' value='$row->tenant'></td></tr>
<tr class='r8'><td>effectdate</td><td><input type=text name='effectdate' value='$row->effectdate'></td></tr>
<tr class='r7'><td>expdate</td><td><input type=text name='expdate' value='$row->expdate'></td></tr>
<tr class='r6'><td>moveindate</td><td><input type=text name='moveindate' value='$row->moveindate'></td></tr>
<tr class='r5'><td>removaldate</td><td><input type=text name='removaldate' value='$row->removaldate'></td></tr>
<tr class='r4'><td>daysleft</td><td><input type=text name='daysleft' value='$row->daysleft'></td></tr>
<tr class='r3'><td>date90</td><td><input type=text name='date90' value='$row->date90'></td></tr>
<tr class='r2'><td>date60</td><td><input type=text name='date60' value='$row->date60'></td></tr>
<tr class='r1'><td>date30</td><td><input type=text name='date30' value='$row->date30'></td></tr>

<tr class='r1'><td></td><td><input type=submit value='Submit'></td></tr>

</table></form>
";

error message:

Fatal error: Uncaught Error: Call to a member function prepare() on null
in C:\xam:6 Stack trace: #0 {main} thrown in C:\xam on line 6

When trying to connect to PDO the connection either returns an object or NULL, the latter indicates the operation failed.

Don’t set error_reporting to anything other then E_ALL and you should have any such setting in the php.ini on your system. By only including those three error types in the setting, you are missing out on php helping you with simple things like typo mistakes.

Since a PDO connection always uses an exception for an error and you didn’t get a Fatal error: … like in the title, for the connection, either the connection was successful and the current error is because $dbo isn’t the name of the variable holding the connection, you managed to set $dbo to a null value after making the connection, or you have a try/catch block (which isn’t needed) around the connection, that isn’t dong anything useful when there is a connection error.

So, correct the error_reporting setting and if that doesn’t give you more information pointing to the problem, you will need to post your code that’s trying to make the connection using PDO.

I prefer using -1 as the parameter because it is easier to type:

Tip
Passing in the value -1 will show every possible error, even when new levels and constants are added in future PHP versions. The E_ALL constant also behaves this way as of PHP 5.4.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.