ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808
Rep:
Perl DBD Postgresql SQL prepare problem
I'm trying to perform a series of selects from a PostgreSQL database using the code below. "$key" is a list of identifiers retrieved from a previous query from a different table.
Code:
$sth = $dbh->prepare( "SELECT path FROM file_paths WHERE key = '?'" );
$sth->bind_param( 1, $key );
$sth->execute;
while ( @rows = $sth->fetchrow_array ) {
# Do something clever with the filepaths returned...
}
(Note: I've omitted the ' || die( $sth->errstr )' portion of several of the statements.)
I'm getting the error:
Code:
Statement has no placeholders to bind at ./dbtest line NN.
where "NN" points to the "bind_param" statement.
Any ideas what the correct syntax of this is supposed to be? All the examples I've run across seem to indicate that my "prepare" is written correctly. If I leave off the single quotes around the '?' placeholder--yes... that wouldn't normally be valid SQL syntax---I get no runtime error but no results from the query either.
I just checked and it looks like you should not quote the question mark (placeholder) -- it works just fine without the quotes. Check the value of the $key variable and make sure your query works in psql.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808
Original Poster
Rep:
Quote:
Originally Posted by firstfire
Hi.
I just checked and it looks like you should not quote the question mark (placeholder) -- it works just fine without the quotes. Check the value of the $key variable and make sure your query works in psql.
It does work without the quotes, after all. It turns out the error was in the way I was interpreting the results of 'fetchrow_array' and how it controlled the while().
Thanks, though, for the assist on the the prepare syntax.
In SQL, '?'(in quotes) is "a literal string that coincidentally consists of a single question-mark."
Without the quotes, the symbol is recognized as a placeholder.
I believe that all SQL engines work this way.
Most do; Some want $ or $1, $2 etc instead... makes hard switching (been there, done that, got the tshirt)
And then there's spring in the java world, to make matters more complex.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808
Original Poster
Rep:
Quote:
Originally Posted by Ramurd
Most do; Some want $ or $1, $2 etc instead... makes hard switching (been there, done that, got the tshirt)
And then there's spring in the java world, to make matters more complex.
Well, the confusion is that the syntax of 'prepare' wants something a little different than one would code if, say, issuing the query directly in psql.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.