phpday 2027 - Call for Papers

pg_select

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_select — Seleziona dei record

Descrizione

function pg_select(
    PgSql\Connection $connection,
    string $table_name,
    array $conditions = [],
    int $flags = PGSQL_DML_EXEC,
    int $mode = PGSQL_ASSOC
): array|string|false

pg_select() seleziona i record specificati da conditions, che ha la forma field=>value. Se la query ha successo, restituisce un array contenente tutti i record e i campi che corrispondono alla condizione specificata da conditions.

Se flags è impostato, pg_convert() viene applicata a conditions con i flag specificati.

Se mode è impostato, il valore restituito sarà sotto forma di array con PGSQL_NUM, di array associativo con PGSQL_ASSOC (predefinito) o di entrambi con PGSQL_BOTH.

Per impostazione predefinita pg_select() passa i valori così come sono. Occorre effettuare l'escape dei valori oppure specificare l'opzione PGSQL_DML_ESCAPE. PGSQL_DML_ESCAPE racchiude tra virgolette ed effettua l'escape di parametri/identificatori. Di conseguenza, i nomi di tabelle/colonne diventano sensibili alle maiuscole/minuscole.

Si noti che né l'escape né le query preparate possono proteggere query LIKE, JSON, Array, Regex, ecc. Questi parametri dovrebbero essere gestiti in base al loro contesto, ad es. effettuando l'escape/la validazione dei valori.

Elenco dei parametri

connection

Un'istanza di PgSql\Connection.

table_name

Nome della tabella da cui selezionare le righe.

conditions

Un array le cui chiavi sono i nomi dei campi della tabella table_name e i cui valori sono le condizioni che una riga deve soddisfare per essere recuperata. A partire da PHP 8.4.0, quando viene fornito un array vuoto, non viene applicata alcuna condizione. In precedenza, la funzione falliva con un argomento conditions vuoto.

flags

Un numero qualsiasi di PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC o PGSQL_DML_STRING, combinati. Se PGSQL_DML_STRING fa parte dei flags, viene restituita la stringa della query. Quando è impostato PGSQL_DML_NO_CONV o PGSQL_DML_ESCAPE, la funzione non chiama internamente pg_convert().

mode

Un numero qualsiasi di PGSQL_ASSOC, PGSQL_NUM o PGSQL_BOTH Se è impostato PGSQL_ASSOC il valore restituito sarà un array associativo, con PGSQL_NUM il valore restituito sarà un array e con PGSQL_BOTH il valore restituito sarà un array sia associativo sia indicizzato numericamente.

Valori restituiti

Restituisce una string se PGSQL_DML_STRING viene passato tramite flags, altrimenti restituisce un array in caso di successo o false in caso di fallimento.

Log delle modifiche

Versione Descrizione
8.4.0 conditions ora è opzionale.
8.1.0 Il parametro connection ora si aspetta un'istanza di PgSql\Connection; in precedenza, era attesa una resource.
7.1.0 È stato aggiunto il parametro mode.

Esempi

Example #1 Esempio di pg_select()

<?php 
  $db = pg_connect('dbname=foo');
  // Questo è abbastanza sicuro, poiché viene effettuato l'escape di tutti i valori.
  // Tuttavia PostgreSQL supporta JSON/Array. Questi non sono
  // resi sicuri né dall'escape né dalle query preparate.
  $rec = pg_select($db, 'post_log', $_POST, PG_DML_ESCAPE);
  if ($rec) {
      echo "Record selezionati\n";
      var_dump($rec);
  } else {
      echo "L'utente deve aver inviato dati errati\n";
  }
?>

Vedere anche:

  • pg_convert() - Converte i valori di un array associativo in forme adatte alle istruzioni SQL

+add a note

User Contributed Notes 2 notes

up
4
david dot tulloh at infaze dot com dot au
21 years ago
Valid options are PGSQL_DML_NO_CONV, PGSQL_DML_EXEC, PGSQL_DMP_ASYNC, PGSQL_DML_STRING (pulled out of source code).

This function does not support selecting from multiple tables.  You can get around this by setting the PGSQL_DML_NO_CONV option.  This prevents the error which occurs when the function tries to convert the condition array.

I think it is also important to point out that the table_name field is not safe, particularily with the PGSQL_DML_NO_CONV option.

The arguements array field is compulsory, as documented.  What isn't so clear is that the array has to actually have some values in it, you can't do a select all.

In summary, this function is good for a very small subset of basic queries.  If you are after anything more complex you are better off with pg_query.
up
-4
wietse at cj2 dot nl
20 years ago
David mentioned that you can't do a Select all.
However, when executing this script:
<?php
    $conn_string = "dbname=mydb";
    $db = pg_connect($conn_string);
    $selectfields = array("imgid" => "");
    $records = pg_select($db,"mmsfiles",$selectfields);
    print_r($records);
?>
...I get this result:
Array
(
    [0] => Array
        (
            [imgid] => 1
            [file] => /home/wietse/public_html/mms/images/1.gif
            [thumb] => 
        )
    [1] => Array
        (
            [imgid] => 2
            [file] => /home/wietse/public_html/mms/images/2.gif
            [thumb] => 
        )
    [2] => Array
        (
            [imgid] => 3
            [file] => /home/wietse/public_html/mms/images/3.gif
            [thumb] => 
        )
    [3] => Array
        (
            [imgid] => 4
            [file] => /home/wietse/public_html/mms/images/4.gif
            [thumb] => 
        )
)
To Top