2
$result = pg_query(Postgres::getInstance(), "SELECT 
date_start, 
date_end, 
cnt_hands, 
cnt_hands_won, 
amt_won, 
id_session,
id_player,
amt_won  
FROM 
cash_table_session_summary 
WHERE 
date_start = '2016-04-27 07:20:47'");

This works perfectly.

echo $sessionStart;
$result = pg_query(Postgres::getInstance(), "SELECT 
date_start, 
date_end, 
cnt_hands, 
cnt_hands_won, 
amt_won, 
id_session,
id_player,
amt_won  
FROM 
cash_table_session_summary 
WHERE 
date_start = $sessionStart");

This throws this:

2016-04-27 07:20:47 Warning: pg_query(): Query failed: ERROR: syntax error at or near "07" LINE 13: ... date_start = 2016-04-27 07:20:47 ^ in /home/haris/public_html/project/DAL_General.php on line 102

Is colon a problem? Do I need to escape it somehow? If so, how? I've google but found nothing about escaping colons.

Saty
  • 22,443
  • 7
  • 33
  • 51
  • 2
    Use it as `date_start = '$sessionStart'"` inside single quotes – Saty Apr 30 '16 at 10:04
  • 1
    Thanks! I'm new to php, and it didn't even occured to me, that that is possible. Can you make an answer, so I can mark it as correct? – user3134759 Apr 30 '16 at 10:08

2 Answers2

1

I had the same issue when I was learning database work, but you need the query to be made in ""'s - Then when referencing a variable or data you need to put it in ''s, example:

$test_query = type_of_connection_query("SELECT * FROM users WHERE id = '1'");

That would not work like:

$text_query = type_of_connection_query("SELECT * FROM users WHERE id = 1");

And of course there are some times when you don't need quotes, like referencing LIMIT's

$text_query = type_of_connection_query("SELECT * FROM users WHERE id = '1' LIMIT 1");
Jack Hales
  • 1,574
  • 23
  • 51
1

Your date must be inside quotes. You need to change

FROM 
cash_table_session_summary 
WHERE 
date_start = $sessionStart

To

FROM 
cash_table_session_summary 
WHERE 
date_start = '$sessionStart'// add quotes here

For more understand about quotes check When to use single quotes, double quotes, and backticks in MySQL

Community
  • 1
  • 1
Saty
  • 22,443
  • 7
  • 33
  • 51