PDO Exception error

I’m not sure how to deal with the code below to use PDO, the error is in the logs but everything seems to be OK on the website.

if ($selectCategory=="Latest Additions") {
$sql.= " c JOIN (SELECT * FROM tbl_hotels WHERE (tbl_hotels.Act_Hot=1) ORDER BY Id_Hot DESC LIMIT 0, 10) sq ON sq.IdCntry_Hot = c.Id_Cntry ". ((!empty($sqlcountries)) ? $sqlcountries : "") ."";

} else {
$sql.= " AND (tbl_hotels.Act_Hot=1) ". ((!empty($sqlcountries2)) ? $sqlcountries2 : "") ." ORDER by tbl_hotels.Id_Hot DESC";	
}

that’s because the website has error display disabled.

OK the error is now showing, but I have tried binding to the statement and it wont work.

then it’s likely that your query is invalid.

I currently have tried to do it like this

$sql.= " c JOIN (SELECT * FROM tbl_hotels WHERE (tbl_hotels.Act_Hot=1) ORDER BY Id_Hot DESC LIMIT 0, 10) sq ON sq.IdCntry_Hot = c.Id_Cntry ". ((!empty(:sqlcountries)) ? :sqlcountriesb : "") ."";
$sql->bindParam(":sqlcountries", $sqlcountries, PDO::PARAM_INT);
$sql->bindParam(":sqlcountriesb", $sqlcountries, PDO::PARAM_INT);
$sql->execute();

But as soon as i change $sqlcountries to :sqlcountries, I get a red error mark in dreamweaver

And the same then for the line below

AND (tbl_hotels.Act_Hot=1) ". ((!empty($sqlcountries2)) ? $sqlcountries2 : "") ." ORDER by tbl_hotels.Id_Hot DESC";	

What makes you think a string is the same as a PDOStatement?

Ah, right OK. I’m getting the error wrong then by the looks

PHP Fatal error: Uncaught exception ‘PDOException’ with message: Syntax error or access violation

A Life Pro Tip: when asking for help on a forum, always post the exact error message as is. Even if you cannot make any use of the certain error message part, other people can find it essential to provide you an assistance.

There should a query part cited in this error message that can give a hint on where to look for the error.

1 Like

This is it in full -

PHP Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘) AND (tbl_hotels.Act_Hot=1) ORDER by tbl_hotels.Id_Hot DESC’ at line 1’ in

Referring to

$sql.= " AND (tbl_hotels.Act_Hot=1) ". ((!empty($sqlcountries2)) ? $sqlcountries2 : "") ." ORDER by tbl_hotels.Id_Hot DESC";

what is the remainder of the SQL?

I had to edit my last post Dormilich sorry

The whole statement is as follows

Select * from tbl_hotels LEFT JOIN tbl_hotntem ON (tbl_hotntem.Id_Hot=tbl_hotels.Id_Hot)  LEFT JOIN tbl_tematics ON (tbl_tematics.Id_Tem=tbl_hotntem.Id_Tem) WHERE (tbl_tematics.Id_Tem=7) AND (tbl_hotels.Act_Hot=1)  ORDER by tbl_hotels.Id_Hot DESC

There are no syntax errors in this query.
Are you sure this error message belongs to it?

and what is the SQL with countries included?

This is what sqlcountries2 is referring too

if (!empty($_GET['countries']) ? $_GET['countries'] : null) {
	 $countriesArray = array();
     foreach($_GET['countries'] as $countries) {	 
	 $countriesArray[] = '\''.$countries.'\'';
     }
	 
	 $countriesData = implode(',', $countriesArray);	 
	 $sqlcountries =  ' WHERE Id_Cntry IN ('. $countriesData .' )';
	 $sqlcountries2 =  ' AND tbl_hotels.IdCntry_Hot IN ('. $countriesData .' )';
}

It would be way easier if you posted the SQL string that you pass to PDO than the SQL creating PHP code.

Sorry guys,

I have taken the very last line of code that outputs that statement with below

$result=$pdo->prepare($mQry);
$result->execute();

echo ($result);

Echo’d

 Select * from tbl_hotels LEFT JOIN tbl_hotntem ON (tbl_hotntem.Id_Hot=tbl_hotels.Id_Hot)  LEFT JOIN tbl_tematics ON (tbl_tematics.Id_Tem=tbl_hotntem.Id_Tem) WHERE (tbl_tematics.Id_Tem=7) AND (tbl_hotels.Act_Hot=1)  ORDER by tbl_hotels.Id_Hot DESC<br />
<b>Catchable fatal error</b>:  Object of class PDOStatement could not be converted to string in <b>\\CSFFILES11\WEBSITES\category-Result.php</b> on line <b>425</b><br />

I don’t see anything wrong with that query.

No me neither, oh well. I’ll just have to leave it, very odd indeed, thanks though again

This is because you can’t do this:

echo ($result);

You could probably var_dump($result) though.