tags:

views:

95

answers:

2

My code handler.php

 ... 
 // independent variables
 $dbHost = "localhost";
 $dbPort = 5432;
 $dbName = "masi";
 $dbUser = "masi";
 $dbPassword = "123456";

 $conn = "host=$dbHost port=$dbPort dbname=$dbName user=$dbUser password=$dbPassword";

$dbconn = pg_connect($conn);

$sql = "SELECT username, passhash_md5, email 
         FROM users$
         WHERE username='a'        // to get these data from lomake.php
         AND email='a' 
         AND passhash_md5='a'";

     $result = pg_query($conn, $sql);
     if(!$result) {
         echo "An error occurred - Masiii!\n";
         exit;
     }
?>

I put "username=a", "email=a" and password="a" to a form which processes the script handler.php in Firefox. I get

Warning: pg_query(): supplied argument is not a valid PostgreSQL link resource in /var/www/sivusto/handler.php on line 56
An error occurred - Masiii!

I am not sure where the bug is, since I use the same syntax as some examples at PHP.net.

How can you solve the bug?

+3  A: 

Your connection is stored in $dbconn, not $conn

This is where you got your connection.

$conn = "host=$dbHost port=$dbPort dbname=$dbName user=$dbUser password=$dbPassword";

$dbconn = pg_connect($conn);

This is where you queried

$result = pg_query($conn, $sql);

It should be

$result = pg_query($dbconn, $sql);

PG_Connect returns a link to the connection, which you stored in $dbconn. You are trying to use your connection string as a resource, which isn't valid.

Chacha102
+2  A: 

You made a typo:

$result = pg_query($dbconn, $sql);

use $dbconn instead of $conn in this line.

Scharrels