Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
— |
php:postgresql [2022/01/10 10:22] (aktuell) webproducer angelegt |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== PostgreSQL-Funktionen in PHP ====== | ||
+ | Das folgendes Beispiel baut eine Verbindung zu einer PostgreSQL-Datenbank auf und führt eine SELECT-Abfrage durch. | ||
+ | |||
+ | ==== Beispiel ==== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | $connection = 'host=192.168.0.0 dbname=postleitzahlen user=pi password=StrengGeheim'; | ||
+ | $con = pg_connect( $connection ); | ||
+ | |||
+ | $result = getOrtsteile( $con, '10115' ); | ||
+ | |||
+ | function getOrtsteile( $dbcon, $plz ) | ||
+ | { | ||
+ | $sql = "SELECT id, ortsteil, geometry FROM geodaten WHERE plz = '%s'"; | ||
+ | $result = pg_query( $dbcon, sprintf( $sql, $plz ) ); | ||
+ | $arr = []; | ||
+ | | ||
+ | while ($row = pg_fetch_assoc( $result) ) { | ||
+ | | ||
+ | foreach( $row as &$col ) | ||
+ | { | ||
+ | $col = utf8_decode( $col ); | ||
+ | } | ||
+ | | ||
+ | $arr[] = $row; | ||
+ | } | ||
+ | | ||
+ | return $arr; | ||
+ | } | ||
+ | ?> | ||
+ | </code> |