I have been stuck on this for a while now, very beginner I know, but couldn't find any similar issues.
I am trying to display my last topic details but I'm getting a warning.
*Warning: pg_exec() [<a href='function.pg-exec'>function.pg-exec</a>]:
Query failed:
ERROR: operator does not exist: character varying = integer LINE 4: WHERE
t_cat = 3 ^
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.*
Any help appreciated
$topicsearh = pg_exec($db,
"SELECT t_id, t_subject, t_date, t_cat
WHERE t_cat = " . $row['s_id'] . "
ORDER BY t_date DESC LIMIT 1"
);
if(!$topicsearh){
echo 'Last topic could not be displayed.';
}
else{
while($trow = pg_fetch_assoc($topicsearh))
echo '<a href="topicview.php?id=' . $trow['t_id'] . '">' . $trow['t_subject'] .
'</a> at ' . date('d-m-Y', strtotime($trow['t_date']));
}
You need to define FROM
table.
Example
SELECT t_id, t_subject, t_date, t_cat FROM TABLE_NAME WHERE...
----------^^^^^^^^^^^^^^^-----
and also con-cat as below.
WHERE t_cat = '". $row['s_id'] ."'
While @Dipesh is right about the missing FROM
clause, the error at hand points to another problem in your query. t_cat
obviously is of type character varying
. Therefore you must compare it to a matching string constant. But you are handing in a numeric constant without single quotes.
PHP (or MySQL) traditionally tend to silently swallow such errors and do what they think "is best" (which is not the best they could do). PostgreSQL luckily doesn't. It forces you to be unambiguous.
Should be:
WHERE t_cat = '" . $row['s_id'] . "' ORDER BY t_date DESC LIMIT 1"
instead of:
WHERE t_cat = " . $row['s_id'] . " ORDER BY t_date DESC LIMIT 1"
Read the chapter Constants in the manual, in particular String Constants and Numeric Constants.