Forum Moderators: coopster & phranque

Message Too Old, No Replies

DBD::MySQL why this doesnt work

         

webguru

10:42 pm on Apr 22, 2003 (gmt 0)

10+ Year Member



$value = "book";

$sth = $dbh->prepare("select id, category from table
where category like?", undef, $value);
$sth->execute();

why can't I use the above query? The error is:

DBI prepare: invalid number of parameters: handle + 3
Usage: $h->prepare($statement [, \%attr]) at ./xxx.cgi line 7.

Glacai

11:57 pm on Apr 22, 2003 (gmt 0)

10+ Year Member



Don't know if this helps I'm farely new to sql, but I always create the statement first:

$act = "select id, category from table where category like $value";
$sth = $dbh->prepare($act);

You may have to put quotes around the $value in $act, \"$value\"

dkubb

12:46 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



Hi,

You need to pass in the value to execute, and
not prepare, like:

 
my $value = 'book';

my $sth = $dbh->prepare('SELECT id, category FROM table WHERE category LIKE ?');

$sth->execute($value);

The reason why you do this is so that you can re-use
the prepared SQL query more than once (if you wish)
giving it different values each time. With some databases
the pre-preparing the queries and reusing them can save
alot of time.

Also unless you're doing any kind of wild-card matching,
you'd probably want to use = rather than LIKE in the
SQL query above.