LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2018, 10:47 PM   #1
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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.

Ideas?

TIA...

--
Rick
 
Old 01-15-2018, 11:27 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.
 
1 members found this post helpful.
Old 01-16-2018, 09:58 AM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by firstfire View Post
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.

I'll mark this one as 'solved'.
 
Old 01-16-2018, 10:27 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 01-26-2018, 11:48 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 01-27-2018, 10:22 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,808

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Ramurd View Post
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.

It's been solved for a while any way.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting SQL Scripts for MS SQL Server to Postgresql or MySQL markush Programming 4 09-12-2013 02:09 PM
Perl DBD::ODBC installation Problem magk Linux - Newbie 4 11-16-2010 07:26 AM
Problem installing Perl DBD::mysql orfiyus Linux - Software 6 03-03-2007 01:52 PM
perl-DBD-MySQL installation problem kartheekpn Linux - Software 3 09-23-2005 03:01 AM
PostgreSQL 8 and DBI, DBD::Pg rnturn Linux - Software 1 02-22-2005 02:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration