Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
postgresql [2011/06/05 13:08] admin |
postgresql [2011/06/05 13:12] (aktuell) admin |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== PostgreSQL ====== | ====== PostgreSQL ====== | ||
| - | [[PHP mit PostgreSQL]] \\ | + | ===== PHP Integration ===== |
| - | ===== PostgreSQL Passwort zurücksetzen ===== | + | |
| - | <code bash> | + | [[http://www.techrepublic.com/blog/howdoi/how-do-i-use-php-with-postgresql/110|Howto use PHP with PostgreSQL]] \\ |
| - | echo "localhost all all trust" | sudo tee -a /etc/postgresql/8.3/main/pg_hba.conf /dev/null | + | |
| - | su - postgres | + | <code php get_data.php> |
| - | psql | + | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
| - | CREATE USER root WITH PASSWORD 'password'; | + | <html> |
| - | ALTER ROLE root SUPERUSER; | + | <head></head> |
| - | \q | + | <body> |
| + | |||
| + | <?php | ||
| + | // attempt a connection | ||
| + | $dbh = pg_connect("host=localhost dbname=test user=postgres"); | ||
| + | if (!$dbh) { | ||
| + | die("Error in connection: " . pg_last_error()); | ||
| + | } | ||
| + | |||
| + | // execute query | ||
| + | $sql = "SELECT * FROM Countries"; | ||
| + | $result = pg_query($dbh, $sql); | ||
| + | if (!$result) { | ||
| + | die("Error in SQL query: " . pg_last_error()); | ||
| + | } | ||
| + | |||
| + | // iterate over result set | ||
| + | // print each row | ||
| + | while ($row = pg_fetch_array($result)) { | ||
| + | echo "Country code: " . $row[0] . "<br />"; | ||
| + | echo "Country name: " . $row[1] . "<p />"; | ||
| + | } | ||
| + | |||
| + | // free memory | ||
| + | pg_free_result($result); | ||
| + | |||
| + | // close connection | ||
| + | pg_close($dbh); | ||
| + | ?> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| </code> | </code> | ||
| + | |||
| + | <code php edit_data.php> | ||
| + | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> | ||
| + | <html> | ||
| + | <head></head> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | if ($_POST['submit']) { | ||
| + | // attempt a connection | ||
| + | $dbh = pg_connect("host=localhost dbname=test user=postgres"); | ||
| + | if (!$dbh) { | ||
| + | die("Error in connection: " . pg_last_error()); | ||
| + | } | ||
| + | | ||
| + | // escape strings in input data | ||
| + | $code = pg_escape_string($_POST['ccode']); | ||
| + | $name = pg_escape_string($_POST['cname']); | ||
| + | | ||
| + | // execute query | ||
| + | $sql = "INSERT INTO Countries (CountryID, CountryName) VALUES('$code', '$name')"; | ||
| + | $result = pg_query($dbh, $sql); | ||
| + | if (!$result) { | ||
| + | die("Error in SQL query: " . pg_last_error()); | ||
| + | } | ||
| + | | ||
| + | echo "Data successfully inserted!"; | ||
| + | | ||
| + | // free memory | ||
| + | pg_free_result($result); | ||
| + | | ||
| + | // close connection | ||
| + | pg_close($dbh); | ||
| + | } | ||
| + | ?> | ||
| + | |||
| + | <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> | ||
| + | Country code: <br> <input type="text" name="ccode" size="2"> | ||
| + | <p> | ||
| + | Country name: <br> <input type="text" name="cname"> | ||
| + | <p> | ||
| + | <input type="submit" name="submit"> | ||
| + | </form> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </code> | ||
| + | |||
| + | ===== Passwort zurücksetzen ===== | ||
| + | |||
| + | echo "localhost all all trust" | sudo tee -a /etc/postgresql/8.3/main/pg_hba.conf /dev/null | ||
| + | |||
| + | su - postgres | ||
| + | |||
| + | psql | ||
| + | |||
| + | CREATE USER root WITH PASSWORD 'password'; | ||
| + | |||
| + | ALTER ROLE root SUPERUSER; | ||
| + | |||
| + | \q | ||