I am stumped, I changed hosts and I am now getting "Call to a member function prepare() on a non-object" on a script that has been working for months on the old server. I have read around here and tried several things I found but nothing seems to make it work again.
I am new at this so speak slowly and clearly please. ;)
$dbh = connect_db();
$sql = 'SELECT count(*) FROM ppipn WHERE txn_id = :txn_id';
$result = $dbh->prepare($sql);
$result->execute(array(':txn_id' => $txn_id));
$number_of_rows = $result->fetchColumn();
if ($number_of_rows>0){
die();
}
The function:
function connect_db()
{
$DbHost = 'localhost';
$DbName = '***n';
$DbUser = '***';
$DbPass = '***';
$table;
try
{
$connection = new PDO('mysql:host='.$DbHost.';dbname='.$DbName, $DbUser, $DbPass);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->setAttribute(PDO::ATTR_PERSISTENT, true);
}
catch (PDOException $e)
{
// Proccess error
echo 'Cannot connect to database: ' . $e->getMessage();
}
return $connection;
}
This error means that $dbh in the line $dbh->prepare() is not an object.
Your connect_db() function can either return a built PDO object, or echo "Cannot connect" and return a variable named $connection, that is empty.
Your connect_db() function is almost certainly unable to connect, and you aren't seeing the error echo'd out.
"Could it be a register_globals issue?????? Although I am leaning towards a server issue myself at this point. If anyone has any ideas what the server issue could be I would love to hear it. host is Bluehost." – user3154948
As per Bluehost's docs https://my.bluehost.com/cgi/help/89Host name = (use the server IP address)
and you're using presently localhost
.
Your previous host's DB settings may have required you to use localhost
, but Bluehost's settings doesn't seem to be the case here. This is normal to change from server to server/hosting services.
The following has been taken from their Web site: https://my.bluehost.com/cgi/help/89
Configuration Settings: Use the following configuration settings for connecting to your database Host name = (use the server IP address) Database name = (cpanelUsername_databaseName) Database username = (cpanelUsername_databaseUsername) Database password = (the password you entered for that database user) MySQL Connection Port = 3306 TCP or UDP, either is fine.
Therefore, change localhost
to the IP address they have assigned for you. Check your Email for any details that were sent to you on signing up, or contact Bluehost in order to find out what the IP address setting is that you are supposed to use.
$DbHost = 'localhost'; // change this
See also: https://my.bluehost.com/cgi/help/2167