Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2005, 11:52 AM   #1
wokar
Newbie
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0 wokar is on a distinguished road
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
wokar is offline   Reply With Quote
Old Mar 15th, 2005, 9:31 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
$conn = pg_pconnect("dbname=AddressBook");

This line is wrong, the connection should include a hostname, username and password as well as a dbname.
__________________

tempest is offline   Reply With Quote
Old Mar 16th, 2005, 10:08 PM   #3
Vorlin
Newbie
 
Join Date: Feb 2005
Posts: 12
Rep Power: 0 Vorlin is on a distinguished road
Quote:
Originally Posted by wokar
Hi,
I'm new to php and i ran into some trouble.

I was trying to connect to my database and output some information


$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
It would be like this (I use PostgreSQL all the time, hehe, love it)...

$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.
Vorlin is offline   Reply With Quote
Old Mar 17th, 2005, 8:51 AM   #4
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Thread moved to the right forum.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC