Jump to content

PEAR need help


AV

Recommended Posts

Hello there,

this is my code:

$dsn = 'mysql://root:@localhost/pred';
$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
);
$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
    die($db->getMessage());
}

//PREPARE QUERY
$sth = $db->prepare("INSERT INTO test SET id=NULL, name='John', surname='Smith' ");
if (PEAR::isError($sth)) {
    die($sth->getMessage());
}

$res =& $db->execute($sth, NULL, 'John', 'Smith');
if (PEAR::isError($res)){
    die($res->getMessage());
}

$db->disconnect();

I written this from manual, and I don't understand one thing. If I have already prepared query, why I have to add data (which I want to put in db) here execute($sth, NULL, 'John', 'Smith')? If I don't put it there, the script will occure an error: DB Error: mismatch.

 

I'd appreciate if somebody could spend some time for me and explain it.

 

Sorry for my english...

Link to comment
https://forums.phpfreaks.com/topic/97406-pear-need-help/
Share on other sites

When you're preparing a query you're not supposed to include the values. You'll rather put in placeholders which will then later be replaced with the data when you're executing the statement.

 

I.e.

$sth = $db->prepare('INSERT INTO test (name, surname) VALUES (?, ?)');

$res = $db->execute('John', 'Smith');

 

You might want to use PDO instead. It comes with PHP by default and since it's written in C it'll run faster than whatever PEAR library you're using.

Link to comment
https://forums.phpfreaks.com/topic/97406-pear-need-help/#findComment-498483
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.