Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

php pdo

Posted on 2013-10-24
21
Medium Priority
?
601 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
19 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 888 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 888 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1112 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1112 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 888 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1112 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 45

Accepted Solution

by:
Chris Stanyon earned 1112 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1112 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 888 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 45

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 111

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 count occurrences of each item in an array.
Suggested Courses

579 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