views:

224

answers:

2

The validation seems to fail for some reason. By the validation, I mean the "if ($result)" -part. How can I correctly validate SQL-query?

$dbconn = pg_connect("host=localhost port=5432 dbname=heoa user=heoa password=123");
$email  = $_POST['login']['email'];

$result = pg_query_params( $dbconn,
                'SELECT user_id
                FROM users
                WHERE email = $1',
                array( $email )
                );
if ($result)
{
        while ( $row = pg_fetch_array( $result ) )
        {
                $user_id = $row['user_id'];
        }
        return $user_id;
}
+2  A: 

I suspect that the return value is integer zero which is also interprested as false in the if statement. By doing "if ($result !== FALSE)" (Note that it should be !== and not !=) you'll verify if the result really is different than FALSEand not just false...

Cellfish
A: 

Should you add apostraphe's around $1 in your SQL query, as in?

SELECT user_id
FROM users
WHERE email = '$1'

I'm not sure how PHP expands the $1 variable.

David Andres
(a) PHP doesn't expand variables in single-quoted strings. (b) Using the `$1` parameters is correct usage in the `pg` API. (c) Don't put query parameters in quotes, because they'll be interpreted as literal strings in the SQL expression, instead of parameters.
Bill Karwin
thanks, I wasn't sure. I haven't coded PHP pages in at least five years so the rust is pretty thick if you get my gist
David Andres