p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: first script with psql


Message #1 by "Srinivasan Prasanna" <sri_core@m...> on Thu, 3 Oct 2002 20:43:36
Hi,
This is my first script to connect to psql db and it is failing. My code :
<?
// database access parameters
// open a connection to the database server
$connection = pg_connect ("host=00.0.000.00 port=5432 dbname=consult 
user=consult password=Phun@c...,");

if (!$connection)
{
	die("Could not open connection to database server");
}

// generate and execute a query
$query = "SELECT * FROM msu9900";
$result = pg_query($connection, $query) or die("Error in query: $query.
" . pg_last_error($connection));

// get the number of rows in the resultset
$rows = pg_num_rows($result);

echo "There are currently $rows records in the database.";

// close database connection
pg_close($connection);

?>
 Error I am getting is:
Fatal error: Call to undefined function: pg_query() 
in /usr/local/tomcat/webapps/ussa/testpage.php on line 19

Any thoughts?
Thanks
Sri
Message #2 by "Srinivasan Prasanna" <Sri_core@m...> on Thu, 3 Oct 2002 20:45:45
This script is based on the tutorial at 
http://www.devshed.com/Server_Side/PHP/PostgreSQL/print_html
Thanks
Sri



> Hi,
T> his is my first script to connect to psql db and it is failing. My 
code :
<> ?
/> / database access parameters
/> / open a connection to the database server
$> connection = pg_connect ("host=00.0.000.00 port=5432 dbname=consult 
u> ser=consult password=Phun@c...,");

> if (!$connection)
{> 
	> die("Could not open connection to database server");
}> 

> // generate and execute a query
$> query = "SELECT * FROM msu9900";
$> result = pg_query($connection, $query) or die("Error in query: $query.
">  . pg_last_error($connection));

> // get the number of rows in the resultset
$> rows = pg_num_rows($result);

> echo "There are currently $rows records in the database.";

> // close database connection
p> g_close($connection);

> ?>
 > Error I am getting is:
F> atal error: Call to undefined function: pg_query() 
i> n /usr/local/tomcat/webapps/ussa/testpage.php on line 19

> Any thoughts?
T> hanks
S> ri
Message #3 by "Nikolai Devereaux" <yomama@u...> on Thu, 3 Oct 2002 13:14:38 -0700
Did you enable the postgres extension in PHP?  It's not built in like it is
with mysql.

Also, you need php version >= 2.0 for pg_query.  That function used to be
called pg_exec() in older versions.

Message #4 by "Srinivasan Prasanna" <sri_core@m...> on Thu, 3 Oct 2002 22:04:23
Hello Nik,
Thanks much. How do I enable pg extension in PHP? I am not the root of 
this db. From what I read in this article 
http://www.joot.com/dave/writings/articles/php/ it has to be done as a 
root to the db.  Also the pg_query change to pg_exec() worked I think. Now 
the error I am getting is 
Resource id #2
Fatal error: Call to undefined function: pg_num_rows() 
in /usr/home/consult/public_html/testpage.php on line 25

Thanks
Sri
> 
Did you enable the postgres extension in PHP?  It's not built in like it is
with mysql.

Also, you need php version >= 2.0 for pg_query.  That function used to be
called pg_exec() in older versions.

Message #5 by "Srinivasan Prasanna" <sri_core@m...> on Thu, 3 Oct 2002 22:08:14
Got this working now. change pg_num_rows() to pg_numrows() and it worked. 
Thanks as lot Nik. 
Thanks much
Sri


> Hello Nik,
T> hanks much. How do I enable pg extension in PHP? I am not the root of 
t> his db. From what I read in this article 
h> ttp://www.joot.com/dave/writings/articles/php/ it has to be done as a 
r> oot to the db.  Also the pg_query change to pg_exec() worked I think. 
Now 
t> he error I am getting is 
R> esource id #2
F> atal error: Call to undefined function: pg_num_rows() 
i> n /usr/home/consult/public_html/testpage.php on line 25

> Thanks
S> ri
>>  
D> id you enable the postgres extension in PHP?  It's not built in like it 
is
w> ith mysql.

> Also, you need php version >= 2.0 for pg_query.  That function used to be
c> alled pg_exec() in older versions.

Message #6 by "Nikolai Devereaux" <yomama@u...> on Thu, 3 Oct 2002 13:59:00 -0700
> Thanks much. How do I enable pg extension in PHP?

You have to configure PHP to include Postgres support when it's compiled.  It
has nothing to do, really, with the database at all -- you're just telling PHP
that you want those functions to be enabled.

> Also the pg_query change to pg_exec() worked I think. Now
> the error I am getting is
> Resource id #2
> Fatal error: Call to undefined function: pg_num_rows()
> in /usr/home/consult/public_html/testpage.php on line 25


Did you read the docs at http://www.php.net/pgsql?  There is a big warning
saying that a bunch of function names have been changed in version 4.2.0.  If
you're using an older version, I'd recommend upgrading.  If you're stuck with
an old version, use the old function names.

They're all listed in a table.


Even if you didn't read the main doc page for pgsql, the doc page for
pg_num_rows also says quite plainly that pg_num_rows is available in PHP
versions greater than 4.2.0.  We've already determined that you're running an
older version.  On that same page, there is an additional note: "This function
used to be called pg_numrows()".

  http://www.php.net/pg_num_rows


take care,

nik


  Return to Index