Solved

php pdo

Posted on 2013-10-24
21
587 Views
Last Modified: 2013-10-24
I am running php 5..2.5 with the following extensions active:

PDO
pdo_mysql
mysqli
mysql

I get this error when using PDO (I have substituted name and pw for the real values which are correct)
:
//Connect to the database $dbh = new PDO("mysql:host=localhost;dbname=dev", "name", "pw"); //prepare the query $stmt = $dbh->prepare("INSERT INTO dynamicrows (row1, row2, row3) VALUES (:var1, :var2, :var3)"); PHP Fatal error: Call to a member function execute() on a non-object in C:\Websites\Dreamweaver\Development\dev\jquery_php_insert_extra_rows WORKING\post_to.php on line 18

Please can someone tell me what I am doing wrong
0
Comment
Question by:doctorbill
  • 7
  • 6
  • 5
  • +1
21 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 222 total points
ID: 39597248
Call to a member function ... on a non-object usually means that object instantiation failed, perhaps for the PDO object.  This article teaches how to test for errors and visualize them.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:doctorbill
ID: 39597325
My system is running php 5.2.5 and sql server version 3.0.37
I have specified the ective extensions above
The config.php file is set to use mysqli at the moment - if I change this to use pdo the extension cannot be loaded

Do I need to install a new version od mysql to use pdo or should my system be able to use the extension
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 222 total points
ID: 39597380
php 5.2.5?  PDO should run on that, but you may have to install the extensions separately.
http://php.net/manual/en/pdo.installation.php

How are you checking to see that the instantiation of the PDO object was successful?

And PHP 5.2x is not supported any more, not even for security.  PHP 5.4 is sufficiently current. I would upgrade if at all possible.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
ID: 39597390
Post the full code of your script (and use the Code button in the editor). If PDO wasn't working then you'd get an error before calling the execute() method (like when trying to connect) so it's probably something simple (typo, variable names etc.)
0
 
LVL 58

Expert Comment

by:Gary
ID: 39597399
Is the db connection in a separate file? Are you including this file?
0
 

Author Comment

by:doctorbill
ID: 39597425
These are enabled:

php_pdo.dll
php_pdo_mysql.dll
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
ID: 39597445
Yeah - got that! You'd generate errors before the execute() if they weren't. Any chance of seeing the code?
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 222 total points
ID: 39597521
Agree with ChrisStanyon -- we need to see the code including the way you've implemented this:
http://php.net/manual/en/pdo.setattribute.php

This is how it's typically done.

// WARNING: AN UNCAUGHT CONNECT ERROR WILL BARK OUT THE DB CREDENTIALS!
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$dsn = "mysql:host=$db_host;dbname=$db_name";
try
{
    $pdo = new PDO($dsn, $db_user, $db_word);
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error('NO PDO Connection', E_USER_ERROR);
}
// OPTIONAL: SHOW THE PDO CONNECTION OBJECT
// var_dump($pdo);

// SET PDO TO TELL US ABOUT WARNINGS OR TO THROW EXCEPTIONS
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

Open in new window

0
 

Author Comment

by:doctorbill
ID: 39597539
//Connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=dev", "user", "password");

//prepare the query
$stmt = $dbh->prepare("INSERT INTO dynamicrows (row1, row2, row3) VALUES (:var1, :var2, :var3)");

<?php
//loop through the POST array
foreach ($_POST['item'] as $info):
	//prepare the data for the INSERT		
	$data = array(
		'var1' => $info['var1'],
		'var2' => $info['var2'],
		'var3' => $info['var3'],
	);

	//execute the query
	$stmt->execute($data);
endforeach;
?>

Open in new window

0
 

Author Comment

by:doctorbill
ID: 39597569
oooops - thanks - I missed that
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
ID: 39597600
Hmm. All looks OK. Add a var_dump after the prepare() call and let us know the output:

var_dump($stmt);

Open in new window

0
 

Author Comment

by:doctorbill
ID: 39597619
//Connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=dev", "username", "password");

//prepare the query
$stmt = $dbh->prepare("INSERT INTO dynamicrows (row1, row2, row3) VALUES (:var1, :var2, :var3)");

var_dump($stmt);

<?php
//loop through the POST array
foreach ($_POST['item'] as $info):
	//prepare the data for the INSERT		
	$data = array(
		'var1' => $info['var1'],
		'var2' => $info['var2'],
		'var3' => $info['var3'],
	);

	//execute the query
	$stmt->execute($data);
endforeach;
?>

Open in new window


Result:
//Connect to the database $dbh = new PDO("mysql:host=localhost;dbname=dev", "username", "password"); //prepare the query $stmt = $dbh->prepare("INSERT INTO dynamicrows (row1, row2, row3) VALUES (:var1, :var2, :var3)"); var_dump($stmt); PHP Fatal error: Call to a member function execute() on a non-object in C:\Websites\Dreamweaver\Development\dev\jquery_php_insert_extra_rows WORKING\post_to.php on line 20
0
 

Author Comment

by:doctorbill
ID: 39597636
not my day - sorry for that
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 278 total points
ID: 39597643
Doh. Just realised what your doing - only part of your script is in the PHP tags. You need it all in the <?php ?> tags!

You have the opening one on line 9 - it needs to go before everything:

<?php
//Connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=dev", "username", "password");

//prepare the query
$stmt = $dbh->prepare("INSERT INTO dynamicrows (row1, row2, row3) VALUES (:var1, :var2, :var3)");

//loop through the POST array
foreach ($_POST['item'] as $info):
	//prepare the data for the INSERT		
	$data = array(
		'var1' => $info['var1'],
		'var2' => $info['var2'],
		'var3' => $info['var3'],
	);

	//execute the query
	$stmt->execute($data);
endforeach;
?>

Open in new window

0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
ID: 39597662
Noticed that the opening <?php tag was in the wrong place in the code I posted to your previous question - sorry about that (just corrected it)

It does highlight the importance of trying to understand the code that we provide, rather than just copy/paste ;)
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 222 total points
ID: 39597698
Installing code without understanding it.  AntiPractice #1

Please wrap the instantiation of the PDO object in the try / catch block, as shown here, and activate the var_dump() statement on line 14.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28276048.html#a39597521
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39597713
@Ray - the connection and prepare() aren't firing as the code was outside of the PHP block!!
0
 

Author Closing Comment

by:doctorbill
ID: 39597714
As always - thanks for a solution
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39597764
SMH.  How could I have overlooked that?  Thanks for the points and good luck with the project, ~Ray
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question