Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.