Jump to content

pg_query


the_oliver

Recommended Posts

Hi all!
Having a problem trying to poll a postgresql database.
Im running the code below, and get the following two error: (wrong line numbers!)

Warning: pg_query(): supplied argument is not a valid PostgreSQL link resource in /var/www/html/helm-associates.net/parts/common.php on line 29

Warning: pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in /var/www/html/helm-associates.net/parts/common.php on line 31


Code:


function is_auth_approved()
        {
        $ip=$_SERVER['REMOTE_ADDR'];

        $query = "SELECT ip FROM member WHERE session = '".session_id()."'";
        $result = pg_query($pg_connection,  $query);

        $count_rows = pg_num_rows($result);

        if ($count_rows > 0)
                {
                for ($i=0; $i<$count_rows; $i++)
                        {
                        $row = pg_fetch_row($result, $i);
                        if ( $row[0] == $ip )
                                {
                                return "1";
                                }
                        else
                                {
                                return "0";
                                }
                        }
                }
                else
                {
                return "0";
                }
}
pg_close($pg_connection);


With connection details in a nother file as follows:

//FOR CONNECTION TO POSTGRESQL DATABASE
$pg_host = "hostdetails";
$pg_user = "username";
$pg_pass = "password";
$pg_db  = "helm_associates";
$pg_connection = pg_connect ("host=$pg_host dbname=$pg_db user=$pg_user password=$pg_pass");
        if (!$pg_connection)
          {
          die("Could not open connection to database server $pg_host");
          }


Sure its just something simple ive missed but not sure what! Thanks.
Link to comment
Share on other sites

change this...

[code]function is_auth_approved()
{
[/code]

to this...

[code]function is_auth_approved()
{
global $pg_connection;
[/code]

or

pass the connection resource to the function!

[code]

$test = is_auth_approved ( $pg_connection );

function is_auth_approved ( $con )
{
        $ip=$_SERVER['REMOTE_ADDR'];

        $query = "SELECT ip FROM member WHERE session = '".session_id()."'";
        $result = pg_query($con,  $query);
}
[/code]


me!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.