php pdo

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
doctorbillTechAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Chris StanyonConnect With a Mentor Commented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
 
doctorbillTechAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ray PaseurConnect With a Mentor Commented:
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
 
Chris StanyonConnect With a Mentor Commented:
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
 
GaryCommented:
Is the db connection in a separate file? Are you including this file?
0
 
doctorbillTechAuthor Commented:
These are enabled:

php_pdo.dll
php_pdo_mysql.dll
0
 
Chris StanyonConnect With a Mentor Commented:
Yeah - got that! You'd generate errors before the execute() if they weren't. Any chance of seeing the code?
0
 
Ray PaseurConnect With a Mentor Commented:
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
 
doctorbillTechAuthor Commented:
//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
 
doctorbillTechAuthor Commented:
oooops - thanks - I missed that
0
 
Chris StanyonConnect With a Mentor Commented:
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
 
doctorbillTechAuthor Commented:
//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
 
doctorbillTechAuthor Commented:
not my day - sorry for that
0
 
Chris StanyonConnect With a Mentor Commented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
 
Chris StanyonCommented:
@Ray - the connection and prepare() aren't firing as the code was outside of the PHP block!!
0
 
doctorbillTechAuthor Commented:
As always - thanks for a solution
0
 
Ray PaseurCommented:
SMH.  How could I have overlooked that?  Thanks for the points and good luck with the project, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.