Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the dollar sign ('$') mean when in the string to .query?

What does the $ sign mean in this statement:

// SQL Query > Update Data
client.query('UPDATE items SET text=($1), complete=($2) WHERE id=($3)',
[data.text, data.complete, id]);
like image 220
na3na3iss Avatar asked Sep 14 '25 15:09

na3na3iss


1 Answers

Pretty universally in that context it's called a "placeholder". You got that code from this blog entry. You can see there that client is defined above in the callback to pg.connect,

pg.connect(connectionString, (err, client, done) => {

Looking up in that blog entry, pg is defined here

const pg = require('pg');

You can always find out what an npm-installed module name resolves to by doing a quick search. In this case though the blog openly says they're using node-postgres. Which documents this under,

  • Paramaterized Queries

Paramaterized means accepting of a parameter. That parameter's placement is defined with a "placeholder" as specified with $. The purpose of this is often to save time in planning and to avert SQL Injection attacks.

like image 150
NO WAR WITH RUSSIA Avatar answered Sep 16 '25 08:09

NO WAR WITH RUSSIA