0

I'm running some PHP code where I am trying to find rows with a specified parameter. I have successfully connected with the database, however I'm running into one problem.

When I run this code:

    $SQL = "SELECT * FROM schema.table";
    $result = pg_exec($dbconn, $SQL);
    while ($row = pg_fetch_array($result) {
          echo "data: " .$row . "<br/>\n";

I get a printed list of arrays showing that I was successful in taking this data from the database and being able to print it out into the website page.

However, I need to filter the data where I only want the rows with a specific ID. When I try this query:

    $SQL = "SELECT * FROM schema.table WHERE 'ID' = '123'";

pg_numrows($result) is 0 when there are four rows with ID of 123 and there is nothing to show and no array and I have no idea why adding the 'where' statement changes anything. What happened to the data, why does filtering it cause it to no longer work?


SOLVED: Figured out the answer for anyone running into the same problem.

    $SQL = "SELECT * FROM schema.table WHERE \"ID\" = '123'";

This is the syntax that must be used for character varying columns. Thanks for everyone making me realize I was running a funny code with wrong quotation marks.

5
  • 1
    Why is your ID surrounded by '', shouldn't it be 123 and not '123'? Commented Jun 23, 2014 at 17:10
  • Backticks and single-quotes and bears, oh my! Commented Jun 23, 2014 at 17:12
  • 2
    String 'ID' is never equal to string '123'. Commented Jun 23, 2014 at 17:13
  • If I don't use the single quotes on ID I get this error: Query failed: ERROR: column "id" does not exist. And when I take off single quotes from 123 I get: Query failed: ERROR: invalid input syntax for integer Commented Jun 23, 2014 at 17:51
  • You could simply write: $SQL = "SELECT * FROM schema.table WHERE ID = '123'"; There's no need for quoting the column name. as @user3763227 wrote in his last comment. Commented Jun 23, 2014 at 18:23

1 Answer 1

2

use like this..

$SQL = "SELECT * FROM schema.table WHERE ID = 123";

Never use single quotes for column names and never use single quotes for int type..

Sign up to request clarification or add additional context in comments.

4 Comments

You should explain what is wrong with the original syntax so the OP understands what the problem is.
If I don't use the single quotes on ID I get this error: Query failed: ERROR: column "id" does not exist. And when I take off single quotes from 123 I get: Query failed: ERROR: invalid input syntax for integer
please provide your table structure ie, type of each column with its name
@user3756998 then try this..$SQL = "SELECT * FROM schema.table WHERE ID = '123'"; use single quotes for 123 only.. since you are using varchar for ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.