pear DB not executing an insert query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

pear DB not executing an insert query

Post by Burrito »

I have a query that looks something like this:

Code: Select all

$qry = "INSERT INTO `myTable`
SET
`myField` = '".$this->dbCleanse($someField)."',
`myField2` = '".$this->dbCleanse($someField2)."'";
then I'm trying to execute that query using pear DB like this:

Code: Select all

$stm = $this->dbConnection->query($qry);
but it's not performing my insert. If I echo my query out and then manually insert it, it runs fine. I'm new to pear DB and am not sure I'm doing this right. I really don't want to use the prepare() and execute() methods because this isn't going to take any extraneous arguements. Can I not use the query() method to perform an insert query?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

check the return value of db::query() to see if it's PEAR_Error instance.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

if(DB::isError($stm))
      {
        echo "problem!";
      }
this isn't yeilding anything

nor is:

Code: Select all

echo $stm;
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

var_dump($stm)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

yields:

Code: Select all

int(1)
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

what is

Code: Select all

$this->dbConnection->query()
for????


you should be able just to instantiate db object and call a method on it

$db = new DB;
$db->query();

what version of PEAR::DB is this???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://pear.php.net/manual/en/package.database.db.db-common.query.php wrote:DB_common::query()
[...]
Return valuemixed - a new DB_result object for queries that return results (such as SELECT queries), DB_OK for queries that manipulate data (such as INSERT queries) or a DB_Error object on failure
Now guess what

Code: Select all

<?php
require 'DB.php';
var_dump(DB_OK);
?>
prints ;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

var_dump(DB_OK);
yields the same thing:

Code: Select all

int(1)
but nothing is getting inserted into my table.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

check your query

Code: Select all

echo $dbObject->last_query;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

also try

Code: Select all

$stm = $this->dbConnection->query($qry); 
if (PEAR::isError($stm)) {
    die('error: '.$stm->getMessage());
}
else if ( DB_OK===$stm ) {
	echo '<div>', $this->dbConnection->affectedRows(), " records have been inserted</div>\n";
}
else {
	die('weird: '.$stm);
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

echoing lastquery didn't do anything.

Volka, yours resulted in:
"records have been inserted"

it didn't indicate a number of rows being inserted, nor did it actually insert a row.

wtf?

edit: I mean wtf as in the real meaning of wtf...not you wtf :P
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

I think you may have it set up all wrong.

where does dbConnect() comes from???

all you had to do with pear is this
config.php

Code: Select all

require_once( 'DB.php' );

$db =& DB::connect( dbdriver://user:password@localhost/database );
$db->setFetchMode( DB_FETCHMODE_ASSOC );

if ( PEAR::isError( $db ) ) 
{
  die( $db->getMessage(  ) );
}

from there

Code: Select all

$result = $db->query('insert into table values( ?, ?, ?)', array( $value, $value, $value));
        if ( PEAR::isError( $result ) ) 
        {
          echo $result->getMessage() );
          echo $db->last_query;
        }
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

uggh...I got it working with prepare() and execute()...but am really frustrated that I couldn't make it work with the query() method.

shouldn't the query() method work with inserts?
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

weirdness.

I guess I'm going to chalk it up to voodoo then. Maybe when I get some more time I'll work it further, but I really need to get this out the door this afternoon and I have it working with prepare() and execute().

thx for trying everyone 8O
Post Reply