-1

I have a postgres query to fetch data from table "like"

Code is

$query = 'select * from like  where (discovery=? and user=?);';
$res = pg_query_params($query, array($_POST['discovery_id'], $_POST['user_id']));

However this on execution give error

<b>Warning</b>:  pg_query_params(): Query failed: ERROR:  syntax error at or near "where"
LINE 1: select * from like  where (discovery=? and user=?);
                        ^ in

How can I execute this query properly? i have another tables named discovery and user. could that be the issue?

Anoop Krishnan
  • 331
  • 3
  • 14

1 Answers1

0

try

$query = 'select * from "like" where (discovery=? and user=?);';
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • its again showing Warning: pg_query_params(): Query failed: ERROR: syntax error at or near "\" LINE 1: select * from \"like\" where (discovery=? and user=?); ^ in – Anoop Krishnan Aug 06 '18 at 13:53
  • Show us your create table for `"like"`? Postgresql is case sensitive. and echo your $query to make sure show `"like"` – Juan Carlos Oropeza Aug 06 '18 at 13:55
  • i have another tables named discovery and user. could that be the issue? – Anoop Krishnan Aug 06 '18 at 13:55
  • Nope that shouldn't be a problem because those are treated as fields. But for testing you can remove the `WHERE` part and the parameters. Again the problem is the back quotes so echo the `$query` variable to make sure you escape the character correctly.Also TRY `select * from "like"` direct on postgresql. – Juan Carlos Oropeza Aug 06 '18 at 13:59
  • of course you should avoid using reserved keywords as table names. – Juan Carlos Oropeza Aug 06 '18 at 14:02
  • You dont have to escape the quote (`\"`) unless you start the string with `"` so just `.. select * from "like" ..` is fine, because well.. literal string. – Xorifelse Aug 06 '18 at 14:06
  • @Xorifelse are you sure? because the line is `$query = 'select * from like ...` – Juan Carlos Oropeza Aug 06 '18 at 14:08
  • @Xorifelse nevermind. You are right I swear i read was `$query = "select * from like ..."` – Juan Carlos Oropeza Aug 06 '18 at 14:09
  • @JuanCarlosOropeza now the error changed to pg_query_params(): Query failed: ERROR: operator does not exist: integer =? LINE 1: select * from "like" where (discovery=? and user=?); ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. in – Anoop Krishnan Aug 06 '18 at 14:19
  • again show us the `CREATE TABLE`. but here look like your parameter are wrong. Are those integer or strings? First try `$query = 'select * from like where (discovery=123 and user=\'somestring\');';` strings in postgresql use single quotes so you have to escape it. – Juan Carlos Oropeza Aug 06 '18 at 14:22
  • got it fixed as $query = 'select * from "like" where (discovery=$1 and user=$2);'; thanks – Anoop Krishnan Aug 06 '18 at 14:24
  • No problem, remember accept the answer so people know the issue is solved. – Juan Carlos Oropeza Aug 06 '18 at 14:29