0

I have question about to_tsquery_partial() function in postgresql, I want to put php variable that reads user input inside this function. I have a problem with syntax, I've tried many times but nothing works. Can you help me please?. Here is the query:

$query='SELECT * FROM planet AS a,to_tsquery_partial(\'{$user}\') AS query
WHERE ts_road @@ query';

Thankx in advance :)

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100

1 Answers1

0

Your problem is not related to postgress, just vanialla php. You are inserting that variable into the string incorrectly. You have 2 options:

Use double quotes:

$query="SELECT * FROM planet AS a,to_tsquery_partial('$user') AS query WHERE ts_road @@ query";

Use single quotes and concatenate:

$query='SELECT * FROM planet AS a,to_tsquery_partial(\'' . $user . '\') AS query WHERE ts_road @@ query';

This explains things more in depth: What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Pevara
  • 14,242
  • 1
  • 34
  • 47
  • First thankx for your help, I tried that but it's not working, I still get no result !! –  Apr 23 '16 at 21:54
  • if you `var_dump` the `$query`, does it look like it is supposed to? If you run that query directly on the database (with something like sequelPro or just from the cli), do you get a result? The actual execution of the query is not in your code snippet, so there is not much we can tell about it... (and I have zero experience with postgres, so I probably wouldn't be able to help anyway, but someone else may) – Pevara Apr 23 '16 at 21:56
  • I tried the second and it's working but the rest of query don't have correct syntax –  Apr 23 '16 at 22:08
  • You help me soo much, thankx :) just I must figure out what this happend –  Apr 23 '16 at 22:11
  • That seems like a problem with the query then. Perhaps you should formulate a new question, but as far as I can tell you now know "How to put php variable inside to_tsquery_partial() function" – Pevara Apr 23 '16 at 22:13
  • Yes I think. Ok I will formulate my question. Thankx so much :D –  Apr 23 '16 at 22:20
  • the problem that query is too long. There is a part of it: $query='SELECT * FROM planet AS a,to_tsquery_partial(\'' . $user . '\') AS query WHERE ts_road @@ query UNION SELECT * FROM road AS a,to_tsquery_partial(\'' . $user . '\') AS query WHERE ts_road @@ query .....'; –  Apr 23 '16 at 23:28
  • I can't help you with postgres. You really should post a new question describing your new problem! – Pevara Apr 23 '16 at 23:44