Solved

php pdo

Posted on 2013-10-24
21
578 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
Comment Utility
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
Comment Utility
Is the db connection in a separate file? Are you including this file?
0
 

Author Comment

by:doctorbill
Comment Utility
These are enabled:

php_pdo.dll
php_pdo_mysql.dll
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
Comment Utility
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
Comment Utility
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
Comment Utility
//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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:doctorbill
Comment Utility
oooops - thanks - I missed that
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
Comment Utility
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
Comment Utility
//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
Comment Utility
not my day - sorry for that
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 278 total points
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 278 total points
Comment Utility
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
Comment Utility
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 42

Expert Comment

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

Author Closing Comment

by:doctorbill
Comment Utility
As always - thanks for a solution
0
 
LVL 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

16 Experts available now in Live!

Get 1:1 Help Now