0

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));
2
  • 1
    You are attempting to mix PDO with the old mysql_*() functions, which is not possible. PDO is a separate API, with a different connect method. Jul 2, 2012 at 17:37
  • 1
    @sixeightzero: I think that's what one would call a minor edit!
    – eggyal
    Jul 2, 2012 at 17:51

2 Answers 2

1

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);
4
  • But pdfstatus is not setting to 1
    – Tom
    Jul 2, 2012 at 17:44
  • @Tom: Try adding $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); before your call to $q->execute();. It will probably point out that you have a syntax error in your query owing to the comma between the first ? and WHERE.
    – eggyal
    Jul 2, 2012 at 17:45
  • Call to a member function setAttribute() on a non-object in /home/content/58/9508458/html/pa/test. php on line 12
    – Tom
    Jul 2, 2012 at 17:48
  • @Tom: That should have been added in conjunction with the change given in my answer above.
    – eggyal
    Jul 2, 2012 at 17:48
0

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

8
  • two errors now Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/content/58/9508458/html/pabrowser/ccc.php on line 10 Fatal error: Call to a member function execute() on a non-object in /home/content/58/9508458/html/pabrowser/ccc.php on line 14
    – Tom
    Jul 2, 2012 at 17:52
  • Given that the OP specifically says "I am trying to move the my sql working to PDO", I don't see how suggesting MySQLi helps (except for your final sentence linking to the PDO section of the manual).
    – eggyal
    Jul 2, 2012 at 17:55
  • Fatal error: Call to a member function execute() on a non-object in /home/content/58/9508458/html/pabrowser/ccc.php on line 13
    – Tom
    Jul 2, 2012 at 17:57
  • From the looks of the OP's code above, I don't think they know the difference between PDO and MySQLi. They tried to adapt their code to syntax closer to MySQLi, hence the suggestion. Jul 2, 2012 at 17:57
  • Call to a member function bind_param() on a non-object in /home/content/58/9508458/html/pabrowser/ccc.php on line 14
    – Tom
    Jul 2, 2012 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.