need a hand with my PHP assignment

Status
Not open for further replies.

Scorpion

Solid State Member
Messages
10
Location
Australia
Ok, what I'm trying to do is connect to a database and run a simple query, but i keep getting "Resource id #3" printed in the browser. I even simplified the query down to a simple select * from table but the message still comes help. Here is the code:

$link=pg_connect("dbname=msg user=user");

//connect to database

$query="select * from messages";

//selects all from the database where the start date (start_date) is the current date calculated above

$result=pg_exec($link,$query);

//execs the query

print($result);

//displays the results in the browser

Please help as I really need this stupid thing to work.:) :)
 
Is pg_exec the same as pg_execute? The PHP manual doesn't show pg_exec is why I ask.

Are you sure the connection is being made? A simple check right after it,

if ( !$link ) echo "No connection to the server."

PHP's site also as an example of using the pg_ stuff here, which also uses the pg_prepare. I haven't done anything with PostgreSQL though...:freak:
 
Our teacher has only taught us about the way I did it. I'll give that a try tomorrow and let you know. Thanks
 
i had that before but it was with cookies or sessions and not saying session_start() or messing up the quotes

just for future reference try using the right syntax for ur code... meaning capitolize the key words and putting `'s outside of your table name and such

Basically from what i see php says "resource id" when it cannot really display the variable? try saying something more like:

echo $result[0];

it also might be relevent to show the table structure / data inside if it was conflicting with something especially quotes
 
Tommyboy is right.

You need to tell the code which row you want the data to come from. Eg. echo $result[0]; will get the data from row 0.


Try:
PHP:
$link=pg_connect("dbname=msg user=user");

//connect to database

$query="select * from messages";

//selects all from the database where the start date (start_date) is the current date calculated above

$result=pg_exec($link,$query);

//execs the query

echo $result[0];

//displays the results in the browser
 
No you don't. Printing the $result variable will likely say what it is (or should be, in this case) - an Array. Granted that's not useful, but it has nothing to do with his error.

With that comment aside, a nice command to know is print_r: it will neatly print out any given array, or if you don't know what it is, var_dump will spit out whatever it's holding.
 
Status
Not open for further replies.
Back
Top Bottom