I'm creating a PHP application which relies on PostgreSQL. Now I have this query:

 $query= 'INSERT INTO "users" ("name", "username", "email", "phone") VALUES (\''. $name .'\', \''. $username .'\', \''. $email.'\', '. $phone .')';

$email, $phone and $name values can be null.

When I run this query, I get a syntax error.

So my question is: to insert a null value, I have to manually write null or am I missing something in the query?

Seems like I am forced to write null as value.


Do not use this statement at all. You are wide open for SQL injection.
Use a prepared statement instead:

pg_prepare($pgconn, 'my_insert', "INSERT ... VALUES ($1, $2, ...)");
pg_query($pgconn, 'my_insert', array( $name, $username, ...));

The PHP manual about pg_query:

This function used to be called pg_exec(). pg_exec() is still available for compatibility reasons, but users are encouraged to use the newer name.


  • pg_exec has been replaced by pg_query and examples in official documentation references to pg_execute instead. Shouldn't this answer be updated? – EAmez May 5 at 13:01
  • 1
    @EAmez: Updated accordingly, thanks. – Erwin Brandstetter May 5 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.