• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Transitioning to PDO/MySQL. Need assistance with if/else statement

Are changes necessary for if/else statements in PHP/PDO?

I am getting the following error:
Parse error: syntax error, unexpected 'catidexp1' (T_STRING)

(Script works fine without if/else statement)

$query3 = $conn->query('SELECT DISTINCT cat1, catidexp1 FROM download ORDER BY catidexp1);

while ($row3 = $query3->fetch(PDO::FETCH_ASSOC))

 if $row3['catidexp1']==0 {      //error here
} else {

$statement = $conn->prepare('INSERT INTO category (category_id) VALUES(:catid)');
$params = array(':catid'=>$row3['catidexp1']);


Open in new window

2 Solutions
Is that your real code?
You are missing the closing apostrophe
$query3 = $conn->query('SELECT DISTINCT cat1, catidexp1 FROM download ORDER BY catidexp1');

Open in new window

Also don't put prepared statements inside a loop, put them before you loop - their purpose is to tell MySQL what to expect, not keep telling it what is coming...

$statement = $conn->prepare('INSERT INTO category (category_id) VALUES(:catid)');
Chris MillardCommented:
Change line 6 to:-

 if ($row3['catidexp1']==0) {
Ray PaseurCommented:
Parse error: syntax error, unexpected ...
always means something is wrong "upstream" from the line with the error.  It may be the line above or a line many lines above.  Typically this is due to missing or unmatched parentheses, quotes, etc.

This has nothing to do with PDO, per se -- it's a PHP syntax error that may have been introduced by a conversion from MySQL to a current SQL extension, but looking to PDO for an answer is a red herring.  Instead, go back to the basic PHP syntax and you'll find the error through that process.
lawrence_devAuthor Commented:
Agreed Gary!  Thanks for your help!  Thanks Chris!
lawrence_devAuthor Commented:
Take 30 seconds to look at the code to see the mistake and point it out. ---  100% Agree!  Thanks again for your help!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now