![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
How do i use pg_query
Hi,
I'm new to php and i ran into some trouble. I was trying to connect to my database and output some information <?php $conn = pg_pconnect("dbname=AddressBook"); if (!$conn) { echo "An error occured.\n"; exit; } $result = pg_query($conn, "SELECT Name, Email FROM Addresses"); if (!$result) { echo "An error occured.\n"; exit; } while ($row = pg_fetch_row($result)) { echo "Name: $row[0] Email: $row[1]"; echo "<br />\n"; } ?> I'm not sure if im using the pg_query command properly. In my database AddressBook, theres a table called Addresses which has Name, Email and phone Number column also what do they mean by row[0] and row [1] because all name phone number and email r in one row.????? each time i do this, i get "An error occured." it doesnt excecute past that... all the help would be appreciated thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
$conn = pg_pconnect("dbname=AddressBook");
This line is wrong, the connection should include a hostname, username and password as well as a dbname.
__________________
|
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Feb 2005
Posts: 12
Rep Power: 0
![]() |
Quote:
$conn = pg_pconnect("dbname=AddressBook");
if (!$conn) {
echo "An error occured.\n";
exit;
}
$result = pg_query("SELECT Name, Email FROM Addresses");
if (!$result) {
echo "An error occured.\n";
exit;
} else {
while ($row = pg_fetch_row($result)) {
$name = $row[0];
$email = $row[1];
echo "Name: $name - Email: $email<br />";
}
}When you pg_fetch_row, everything you're calling is an array, so element 0 would be the Name you called and element 1 would be the Email. If you want to call it by association, do this (shortened code): while ($row = pg_fetch_assoc($result)) {
$name = $row['Name'];
$email = $row['Email'];
echo "Name: $name - Email: $email<br />";
}Hope this helps! And not to sound too contrite, but Postgresql's pg_connect or pg_pconnect (make sure you need a persistent connection) does not require a username nor password. By default, it will try to connect to the required database as the user of the process (which is generally nobody because that's the webserver's default user) and unless your db is password protected, that's not needed at all, unlike mysql's functions (to my knowledge, but I don't use it).
__________________
--Vorlin "Systems administration - it's not just my job, it's how I buy video games!" Last edited by Vorlin; Mar 16th, 2005 at 10:14 PM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|