Sampad Das
Sampad Das

Reputation: 139

Postgres query in php with a date variable

Can you please help me on below query?

php code://

$countdate='2017-01-03';
$countsql='SELECT rucid,"databaseType","countLoggedOn","prodCount","nprodCount","countType" FROM "ru_countLog" WHERE "countLoggedOn"=$countdate';

--> It's giving syntax error

syntax error at or near "$" LINE 1: ...untType" FROM "ru_countLog" WHERE "countLoggedOn"=$countdate

Upvotes: 0

Views: 555

Answers (2)

Seth Difley
Seth Difley

Reputation: 1330

Remove the internal double quotes from your query:

$countsql = "SELECT rucid, databaseType, countLoggedOn,
             prodCount, nprodCount, countType
             FROM ru_countLog
             WHERE countLoggedOn = $countdate";

Note that this query is vulnerable to SQL injection. Consider parametrizing $countdate. With http://php.net/manual/en/function.pg-query-params.php, this would become

$countsql = 'SELECT rucid, databaseType, countLoggedOn,
         prodCount, nprodCount, countType
         FROM ru_countLog
         WHERE countLoggedOn = $1';

$result = pg_query_params($dbconn, $countsql, array($countdate));

where $dbconn is your database connection

Upvotes: 1

gobliggg
gobliggg

Reputation: 247

Maybe you should try like this

$countdate='2017-01-03';
$countsql='SELECT rucid,"databaseType","countLoggedOn","prodCount","nprodCount","countType" FROM "ru_countLog" WHERE "countLoggedOn"='.$countdate;

Hope this help

Upvotes: 0

Related Questions