Tom
Tom

Reputation: 244

Fatal error: Call to a member function prepare() on a non-object PDO

I am trying to move the my sql working to PDO but it showing the following Fatal error: Call to a member function prepare() on a non-object in /home/content/58/9508458/html/pa/test. php on line 12

orginal code

<?php
$pdfreq=$_GET['dadi'];
include('config.php');
require('fpdf.php');
$link =mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);



$update = "UPDATE mastertable SET pdfstatus =1 WHERE id_pk = $pdfreq";
mysql_query($update, $link);

Replaced with

<?php
$pdfreq=$_GET['dadi'];
include('config.php');
require('fpdf.php');
$link =mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);



$sql = "UPDATE mastertable SET pdfstatus=?, WHERE id_pk=?";
$q = $link->prepare($sql);
$q->execute(array(1,$pdfreq));

Upvotes: 0

Views: 3094

Answers (2)

Mike Mackintosh
Mike Mackintosh

Reputation: 14237

You are using MySQL helper functions, not PDO or MySQLi.

Prepare will not work with what you are doing

You need to change your syntax to, note the trailing i in MySQLi

<?php
$pdfreq=$_GET['dadi'];
$id = 1;
include('config.php');
require('fpdf.php');
$link = new mysqli($db_host,$username,$password, $db_name);

$sql = "UPDATE mastertable SET pdfstatus=? WHERE id_pk=?";
$q = $link->prepare($sql);

$q->bind_param("is", $id, $pdfreq);
$q->execute();

Since MySQL does not have a prepare method, your value will never get set to 1, and you will fail with Fatal error: Call to a member function prepare() on a non-object

If you wish to use PDO see the PDO Documentation on PHP.Net

Upvotes: 0

eggyal
eggyal

Reputation: 125865

Your $link variable is not a PDO object. You should replace:

$link = mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);

With:

$link = new PDO("mysql:dbname=$db_name;host=$db_host",$username,$password);

Upvotes: 1

Related Questions