pg_fetch_assoc

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

pg_fetch_assocВыбирает строку результата запроса и помещает данные в ассоциативный массив

Описание

array pg_fetch_assoc ( resource $result [, int $row ] )

pg_fetch_assoc() возвращает ассоциативный массив содержащий записи из строки результата запроса.

Результат выполнения pg_fetch_assoc() тот же, что и у pg_fetch_array() с параметром PGSQL_ASSOC. Функция возвращает только ассоциативный массив. Если нужен численно индексированный массив, используйте функцию pg_fetch_row().

Замечание: Эта функция устанавливает NULL-поля в значение NULL PHP.

pg_fetch_assoc() не намного медленней и значительно проще в использовании, чем pg_fetch_row().

Список параметров

result

Ресурс результата запроса PostgreSQL, возвращаемый функциями pg_query(), pg_query_params() или pg_execute() (в числе прочих).

row

Номер выбираемой из результата запроса строки. Нумерация начинается с нуля. Если аргумент опущен или равен NULL, берется следующая по очереди строка.

Возвращаемые значения

Ассоциативный массив, индексированный именами полей выборки. Значения массива представляются в виде текстовых строк. Значения NULL базы данных преобразуются в PHP NULL.

FALSE, когда row превышает число строк в результате запроса, когда строк в результате не осталось, и при прочих ошибках.

Примеры

Пример #1 Пример использования pg_fetch_assoc()

<?php 
$conn 
pg_connect("dbname=publisher");
if (!
$conn) {
  echo 
"Произошла ошибка.\n";
  exit;
}

$result pg_query($conn"SELECT id, author, email FROM authors");
if (!
$result) {
  echo 
"Произошла ошибка.\n";
  exit;
}

while (
$row pg_fetch_assoc($result)) {
  echo 
$row['id'];
  echo 
$row['author'];
  echo 
$row['email'];
}
?>

Смотрите также

  • pg_fetch_row() - Выбирает строку результата запроса и помещает данные в массив
  • pg_fetch_array() - Возвращает строку результата в виде массива
  • pg_fetch_object() - Выбирает строку результата запроса и возвращает данные в виде объекта
  • pg_fetch_result() - Возвращает запись из результата запроса

Коментарии

Автор:
At a glance, the syntax listed at the top of this page doesn't match the example.  The PGSQL_ASSOC flag isn't necessary.
2003-01-06 19:53:56
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
If you are moving between different versions of PHP, this might be handy:

if (!function_exists('pg_fetch_assoc')) {
    function pg_fetch_assoc ($result)
    {
      return @pg_fetch_array($result, NULL, PGSQL_ASSOC);
    }
}
2003-06-21 11:29:12
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
An important thing to note (as of PHP 4.3.2):

If you are used to using the "extended" comparision operators (=== and !==) to try to make your code easier to follow visually, this function will return NULL if the provided resource handle is invalid (as opposed to false). ie,

$rs = @pg_query('SELECT * FROM fake_table');
while (false !== ($row = @pg_fetch_assoc($rs)))
{
    print_r($row);
}

Obviously you should check to see if $rs === false before you start the while loop, but this example is used to illustrate a potential infinite loop problem if $rs IS false.
2003-10-24 21:35:10
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
$dbconn3 = pg_connect("host=127.0.0.1 port=5432 dbname=blah user=blah password=blah");
$result = pg_query($dbconn3, "SELECT * FROM Packages");

 echo "<HTML><HEAD><TITLE>PostgreSQL Test Page</TITLE></HEAD><BODY>";
 echo "<TABLE>";

$pkg = pg_fetch_assoc($result);
foreach ($pkg as $value) {
    echo "<TR><TD>$value";
    echo "</TR></TD>";
 }

echo "</TABLE><P>";
echo "This package's full filename is: {$pkg['name']}-{$pkg['version']}{$pkg['extension']}";
echo "</BODY></HTML>";

For generating tables, this works, and personally I prefer foreach() to while loops because there's no danger of accidentally causing an infinite loop...foreach only works for as long as it has something to work with, and then stops.  I thought the echo down the bottom might come in handy, too...took me a bit to find that out.
2005-02-24 21:22:46
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
Note:

PostgreSQL boolean values set to TRUE are returned as the string "t"

PostgreSQL boolean values set to FALSE are returned as the string "f"
2005-09-22 09:34:43
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
If you request a row that does not exist, it just fails, rather than simply returning false.
2006-05-24 15:59:21
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
Here is another way to iterate a resultset and display all columns in very little code... might be faster than a foreach

<?php

print '<table>';
while(
$row=pg_fetch_assoc($rs2)) print '<tr><td>'.join('</td><td>',$row2).'</td></tr>';
print 
'</table>';

?>
2007-03-01 11:28:56
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Is worth to know that when you query on multiple tables only the first row with each name is returned.

That is, if you are joining to tables with a column called 'name' you will receive only one field called name in the array and it will correspond to the one on the first table.

Is advisable to allways allias your columns in that stuation.
2007-10-02 12:57:58
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
In a bit of follow-up to Luke's note about SQL booleans (this was a painful thing to learn the hard way), a relatively easy workaround is to typecase the boolean columns to integer inside the query, e.g:

<?php
// Assuming 'foo' is a table column of type boolean
$res pg_query("Select foo as foo1, foo::integer as foo2 from bar");

$data pg_fetch_assoc($res);
if (
$data['foo1']) echo 'foo1 = TRUE'// Doesn't work as expected (string 't' and string 'f' both evaluate as TRUE)
if ($data['foo2']) echo 'foo2 = TRUE'// Works as expected (string '0' evaluates as FALSE)
?>
2008-08-26 18:01:00
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
regarding the optional int parameter

requesting a row number that is not present in the result set is an error. don't do it.

check with pg_num_rows() beforehand, or  just use the default behavior which returns the rows in order and false after returning the last row it returns false immediately if no rows were returned.
2008-10-13 19:03:30
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
bytea columns are returned escaped.
you need to call pg-unescape-bytea() on them to get the original binary back.
2008-10-13 20:18:37
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
Beware!  If your query returns multiple columns with the same names, only the right-most one will be contained in the result array.  This can cause problems if you are using a combination of joins:

For example:
<?php
// Let's say that 'pkey' is the primary-key colum for tables a and b (primary keys are never null)
$res pg_query("Select a.pkey, b.* FROM a LEFT JOIN b using (pkey)");
$data pg_fetch_assoc($res);
var_dump($data['pkey']) // Is actually b.pkey, may be NULL!
?>

Both tables contain a column named 'pkey'.  Now table 'b' is on the optional side of a LEFT JOIN, so b.pkey (implicitly included via 'b.*') may be NULL.

The problem arises when you use pg_fetch_assoc(), there are two columns named 'pkey' but the result array can only contain one value per key -- in this case it will pick the one from table B instead of the one from table A, and since B is on the optional side of the left-join, $data['pkey'] may be NULL.  So if you're expecting to retrieve the column from table A, you need to use a different pg_fetch() or rewrite your query to avoid ambiguity.
2009-05-19 11:24:27
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Автор:
Be aware that if one of your result fields is an array, it will be output as a string using the general format of '{value1,value2, ... }' in accordance with postgres's behavior with SQL arrays.
http://www.postgresql.org/docs/8.4/static/arrays.html#ARRAYS-IO

So, here is a function to convert simple (one-dimensional) SQL arrays to PHP arrays:

<?php
function pg_parse_array($field)
/*
 * Converts a simple SQL array field to its PHP equivalent.  e.g:
 *
 *    {null}              --> Array(null);
 *    {"null"}            --> Array("null");
 *    {foo,bar}           --> Array("foo", "bar"); 
 *    {"foo,bar"}         --> Array("foo,bar");
 *    {"Hello \"World\""} --> Array('Hello "World"');
 *       
 */ 
{
 
// NULL fields are always NULL
 
if (!is_string($field)) return $field;

 
// Check for curly braces which may indicate an SQL array field
 
if ($field[0] != '{' or substr($field, -1) != '}') return $field;
 
$field trim(substr($field1, -1));

 
$array = Array();
 
 
// Break up the string into the following:
  //  - quoted text that MAY have special chars escaped by a backslash
  //  - unquoted text that may NOT have special chars
 
$search '/(")?+((?(1)(?:\\\\.|[^"])*|[^,]+))(?(1)\\1)/';
 
preg_match_all($search$field$matchesPREG_SET_ORDER);
 
  foreach(
$matches as $value)
  {
    if (
$value[1])
    {
     
// Quoted element, with backslash used to escape chars
     
$array[] = preg_replace('#\\\\(.)#''$1'$value[2]);
    }
    else
    {
     
// Unquoted element
     
$value[2] = trim($value[2]);
      if (
strtolower($value[2]) == 'null'$array[] = null// NULL
     
else $array[] = $value[2];
    } 
  }
  return 
$array;
}

// Some tests to demonstrate this function
var_export(pg_parse_array('{null}'); // Output is Array(null);
var_export(pg_parse_array('{foo,bar}'); // Output is Array('foo', 'bar');
var_export(pg_parse_array('{"null"}'); // Output is Array('null');

?>
2010-01-16 05:16:13
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Here is much powerful pg_parse_array() variant, based on FSM: for any dimension Postgres arrays (its string representation must be well-formed), with quotation rules checks, complexity O(N), where N is a length of string representation of Postgres array:

<?php

define
('STATE_BEGIN'1);
define('STATE_INARRAY',2);
define('STATE_OUTARRAY'3);
define('STATE_INSLASH'4);
define('STATE_INQUOTES'5);

function 
pg_parse_array($value) {
       
$resultArray $indexArray = array(); $level $index 0;
       
$ptr = &$resultArray;
        for(
$i 0$i strlen($value); $i++){
            switch(
$level){
                case 
1:
                    if(
$index 0){
                       
$ptr = & $ptr[sizeof($ptr)];
                    }
                   
$indexArray[++$index] = & $ptr;
                    break;
                case -
1:
                   
$ptr = & $indexArray[--$index];
                    break;
            }
           
$level processFSM($value{$i}, $ptr);
        }
        return 
$resultArray;
    }
   
    function 
processFSM($chr, &$result){
        static 
$state STATE_BEGIN$index 0;
       
$level 0;
        switch(
true){
            case 
$chr == '{' && in_array($state, array(STATE_BEGIN,STATE_INARRAY,STATE_OUTARRAY), true):
               
$state STATE_INARRAY;
               
$index 0;
               
$level = +1;
                break;
            case 
$chr == '}' && in_array($state, array(STATE_INARRAY,STATE_OUTARRAY), true):
               
$state STATE_OUTARRAY;
               
$level = -1;
                break;
            case 
$chr == '\\' && $state !== STATE_BEGIN:
               
$state $state === STATE_INSLASH STATE_INQUOTES STATE_INSLASH;
                break;
           
            case 
$chr == '"' && !in_array($state, array(STATE_BEGIN,STATE_INSLASH), true):
               
$state $state === STATE_INQUOTES STATE_INARRAY STATE_INQUOTES;
                break;
           
            case 
$chr == ',' && in_array($state, array(STATE_INARRAY,STATE_OUTARRAY), true):
               
$index sizeof($result);
                break;
           
            case 
$state !== STATE_BEGIN:
               
$state $state === STATE_INSLASH STATE_INQUOTES $state;
                isset(
$result[$index]) or $result[$index] = '';
               
$result[$index] .= $chr;
                break;
        }
        return 
$level;
    }

?>
2010-03-10 06:54:10
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Note that all pg_fetch_* function ignoring the original type of the data and always return strings. (for numbers too)
2012-06-08 05:37:28
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
<html>
<head>
<script>
function waarde(){
    var text = document.getElementById("optVakken").value;
    document.getElementById("txthidden").value = text;
    document.forms["hiddenform"].submit();
}
</script>
<?php
// keuzemenu met alle leerkrachten
function leerkrachten($aName){
    include(
"includes/connect.php");
   
}
// keuzemenu met alle vakken
function vakken($aID){
    include(
"includes/connect.php");
   
$SelectVakkenQuery "SELECT * FROM vakken";
   
$SelectVakkenResult $mysqli->query($SelectVakkenQuery);
   
$Choice "<select id='$aID' onchange=waarde()><option>Kies een vak</option>";
   
        while(
$rij2 $SelectVakkenResult->fetch_assoc()){
       
$VakID $rij2['vakid'];
       
$Vaknaam $rij2['voluit'];
       
$Choice .= "<option value='$VakID'>$Vaknaam</option>";
    }
   
$Choice .= "</select>";
    return 
$Choice;
}
?>
<title>Remediëringsoefening</title>
</head>
<body>
<?php
include("includes/connect.php");
// aanmaken van keuzemenus

// eerste keuzemenu
echo vakken("optVakken")."<br><br>";

// tweede keuzemenu
if(!isset($_POST['txthidden'])){
$SelectLeerkrachtenQuery "SELECT * FROM leerkrachten";
$SelectLeerkrachtResult $mysqli->query($SelectLeerkrachtenQuery);
   
$Choice "<select>";
while(
$rij=$SelectLeerkrachtResult->fetch_assoc()){
   
$Voornaam $rij['voornaam'];
   
$Naam $rij['naam'];
   
$LKID $rij['leerkrachtid'];
   
$Volledig $Voornaam " " $Naam;
   
$Choice .= "<option value='$LKID'>$Volledig</option>";
}
$Choice .= "</select><br><br>";
echo 
$Choice;
}else{
   
$vakid $_POST['txthidden'];
   
$SelectLeerkrachtenQuery "SELECT * FROM leerkrachten JOIN leerkrachtpervak ON leerkrachten.leerkrachtid = leerkrachtpervak.leerkrachtid WHERE vakid = '$vakid'";
   
$SelectLeerkrachtResult $mysqli->query($SelectLeerkrachtenQuery);
   
$Choice "<select>";
    while(
$row3=$SelectLeerkrachtResult->fetch_assoc()){
       
$Voornaam $row3['voornaam'];
       
$Naam $row3['naam'];
       
$Volledig $Voornaam " " $Naam;
       
$Choice .= "<option>$Volledig</option>";
    }
   
$Choice .= "</select><br><br>";
    echo 
$Choice;
}
// onzichtbaar textbox voor JS
echo "<form method='post' id='hiddenform'><input type='hidden' name='txthidden' id='txthidden'></form>";
$mysqli->close();
?>
</body>
</html>
2015-04-23 23:46:28
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html
Converts 't' and 'f' to PHP Boolean

$result = pg_query($_db, $sql);

while ( $row = pg_fetch_assoc( $result ) )
{
    fixBooleans($result, $row);

    //some other code
}

function fixBooleans($result, &$row)
{

    for ($fld_i = 0; $fld_i < pg_num_fields($result); $fld_i++)
    {
        $fld_name = pg_field_name($result, $fld_i);

        if( pg_field_type($result, $fld_i) == 'bool' )
        {
            if( $row[ $fld_name ] == 't' )
            {
                $row[ $fld_name ] = true;
            }
            elseif($row[ $fld_name ] == 'f')
            {
                $row[ $fld_name ] = false;
            }
        }
    }

}
2020-02-17 18:17:05
http://php5.kiev.ua/manual/ru/function.pg-fetch-assoc.html

    Поддержать сайт на родительском проекте КГБ