Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
Why is your ID surrounded by '', shouldn't it be 123 and not '123'? –  Sefam Jun 23 at 17:10
    
Backticks and single-quotes and bears, oh my! –  Jay Blanchard Jun 23 at 17:12
2  
String 'ID' is never equal to string '123'. –  bloodyKnuckles Jun 23 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 –  user3756998 Jun 23 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. –  VMai Jun 23 at 18:23

1 Answer 1

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..

share|improve this answer
    
You should explain what is wrong with the original syntax so the OP understands what the problem is. –  Mike Brant Jun 23 at 17:32
    
@Mike Brant thanks for your comment i explained –  user3763227 Jun 23 at 17:37
    
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 –  user3756998 Jun 23 at 17:53
    
please provide your table structure ie, type of each column with its name –  user3763227 Jun 23 at 17:57
1  
@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 –  user3763227 Jun 23 at 18:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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