Solved

php pdo

Posted on 2013-10-24
21
583 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 108

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 108

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
 
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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Undefined Index in HTML Form? 2 32
PHP preg_replace code convert to Delphi 14 37
Make custom query_posts look show the excerpt only 9 26
Checking if varaible is empty 6 32
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now