Foren-Übersicht Daten auslesen aus der Datenbank mit einem Dropdown-Menu

Daten auslesen aus der Datenbank mit einem Dropdown-Menu

elexis_linux Site Admin

Beiträge: 396
Website: http://ModernHosting.ch
HIer ist die klasse die eingebunden wird. zb. psql.php

Code: Alles auswählen

<?PHP Class Lib_Psql { var $config; var $psql_connection_error; var $psql_connection; var $psql_last_error; var $dbhost; var $dbname; var $dbuser; var $dbpswd; /************************************************************************************************************** * Konstruktor Der Konstruktor ist im prinzip dazu gedacht, die verschiedenen Parameter zu übergeben, um * Später mit einem "connect()" die Connection aufzubauen. Die Mitgegebenen Werte werden in * die Klassenvariablen gespeichert *************************************************************************************************************/ public function Lib_Psql() { $this->dbhost = PSQL_HOST; $this->dbname = PSQL_NAME; $this->dbuser = PSQL_USER; $this->dbpswd = PSQL_PASSWD; $this->connect(); } public function connect() { $this->psql_connection = pg_connect("host = " . $this->dbhost . " dbname = " . $this->dbname . " user = " . $this->dbuser . " password = " . $this->dbpswd) OR die($this->psql_connection_error = pg_last_error()); if(empty($this->psql_connection_error)) { return TRUE; }else { return FALSE; } } private function _convertResult($result, $sort, $index) { if(($num_rows = pg_num_rows($result)) > 0) { if($sort == ROWS) { $temp = pg_fetch_all($result); }else { $num_fields = pg_num_fields($result); $key = array_keys(pg_fetch_array($result, NULL, PGSQL_ASSOC));// print_r($key); for($i = 0; $i < $num_fields; $i++) { $col = pg_fetch_all_columns($result, $i); for($ii = 0; $ii < $num_rows; $ii++) { if($index == KEYS) $temp[$key[$i]][$ii] = $col[$ii]; //Add Alphanumeric else if($index == NUMERIC) $temp[$i][$ii] = $col[$ii]; //Add Index } } } return $temp; }else { return NULL; } } public function query($query, $sort = ROWS, $index = KEYS) { if($result = pg_query($this->psql_connection, $query)) { return $this->_convertResult($result, $sort, $index); }else { $this->psql_last_error = pg_last_error(); return FALSE; } } public function select($from, $what, $where = NULL, $sort = ROWS, $index = KEYS) { $query = "SELECT " . $what . " FROM " . $from; if($where != NULL) $query = $query . " WHERE " . $where; $query = $query . ";"; if($result = pg_query($this->psql_connection, $query)) { return $this->_convertResult($result, $sort, $index); }else { $this->psql_last_error = pg_last_error(); return FALSE; } } public function insert($table, $columns, $values) { $query = "INSERT INTO " . $table . "( " . $columns . " ) VALUES ( " . $values . " );"; if(pg_query($this->psql_connection, $query)) { return TRUE; }else { $this->psql_last_error = pg_last_error(); return FALSE; } } public function update($table, $set, $where) { $query = "UPDATE " . $table . " SET " . $set . " WHERE " . $where . ";"; if(pg_query($this->psql_connection, $query)) { return TRUE; }else { $this->psq_last_error = pg_last_error(); return FALSE; } } public function delete($table, $where = NULL) { $query = "DELETE FROM " . $table; if($where != NULL) $query = $query . " WHERE " . $where; $query = $query . ";"; if(pg_query($this->psql_connection, $query)) { return TRUE; }else { $this->psql_last_error = pg_last_error(); return FALSE; } } public function close() { return pg_close($this->psql_connection); } public function isConnected() { if($this->getConnectionStatus() == PGSQL_CONNECTION_BAD) { return FALSE; } return TRUE; } public function getConnectionStatus() { return pg_connection_status($this->psql_connection); } public function getConnectionError() { return $this->psql_connection_error; } public function getLastError() { return $this->psql_last_error; } } ?>
Hier list er alle Daten aus der Datenbank(mit PSQL)

Code: Alles auswählen

<?php $psql = new Lib_Psql(); $flag = $psql->select('flags', 'name_flag', '',COLS); $name_flag = $flag['name_flag']; ?>
zeigt er alle gefunden daten au der Datenbank an

Code: Alles auswählen

<select size="1" name="flag"> <?PHP for($i = 0; $i < count($name_flag); $i++) { echo "<option value='" . $name_flag[$i] . "'>" . $name_flag[$i] . "</option>"; } ?> </select>

4 poziom opinie

Zurück zu „PHP Programmieren“



Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 0 Gäste