Understanding Parameters in Postgres As a Team in PHP

How can you use options in a Postgres command Like?

I am reading this document . I am trying to find a word loremin a table questionsand in its column body.

I am unsuccessfully running the following code inside PHP

$result = pg_query_params ( $dbconn, 
    "SELECT question_id, body
    FROM questions
    WHERE body ilike '%$1%'",
    array ( $_GET['search'])                                                                                                                                                       
);

I get this warning

Warning: pg_query_params () [function.pg-query-params]: Request failed: ERROR: bind messages 1 parameters, but prepared expression "" requires 0 in / var / www / codes / search _body.php on line 10 Stack call

+3
source share
2 answers

You should use a parameter instead of an integer value, not inside quotation marks.

1: LIKE :

$result = pg_query_params ( $dbconn, 
    "SELECT question_id, body
    FROM questions
    WHERE body ilike $1",
    array ( "%" . $_GET['search'] . "%")
);

2: LIKE SQL:

$result = pg_query_params ( $dbconn, 
    "SELECT question_id, body
    FROM questions
    WHERE body ilike '%' || $1 || '%'",
    array ( $_GET['search'] )
);
+7

, $1 , , .

Try:

$result = pg_query_params ( $dbconn, 
    "SELECT question_id, body
    FROM questions
    WHERE body ilike $1",
    array ('%' . $_GET['search'] . '%')                                                                                                                                                       
);
+2

Source: https://habr.com/ru/post/1715960/


All Articles