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 have this simple PHP/MYSQL prepare statement:

$DBH->prepare("SELECT * FROM books '.($_POST['author'] ? '' : '').'
               WHERE id = '1'");

The ternary operator above leads to a syntax error. The error is related to using the post variable $_POST['author'].

I tried changing it to:

$_POST[\'author\']
$_POST[\"author\"]
$_POST["author"]

All of the above didn't work.

share|improve this question

put on hold as off-topic by stealthyninja, Jocelyn, andrewsi, mehdi lotfi, Shankar Damodaran Aug 3 at 3:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – stealthyninja, Jocelyn, andrewsi, mehdi lotfi, Shankar Damodaran
If this question can be reworded to fit the rules in the help center, please edit the question.

    
What are you trying to achieve? Your ternary operator has the same result either way. And what is $_POST['author']? A number? A TRUE/FALSE? –  Dan Aug 2 at 16:46
    
what is it author? table? data? –  David Aug 2 at 16:48

5 Answers 5

up vote 2 down vote accepted

Your quotes are mismatched.

$DBH->prepare("SELECT * FROM books ".($_POST['author'] ? '' : '')." WHERE id = '1'");
share|improve this answer

You need to wrap the php in double quotes:

$DBH->prepare("
  SELECT * 
  FROM books " .($_POST['author'] ? '' : ''). " 
  WHERE id = '1'
");
share|improve this answer

It is because you are using " at first, and then '.

"string' needs to be "string" or 'string'

share|improve this answer

i think you cannot use question mark inside a prepare() as it may consider it as a bind variable.

instead use

$author = $_POST['author'] ? '' : '';
$Sql = "SELECT * FROM books.$author WHERE id = 1";
$DBH->prepare($Sql);

Hope it works!

share|improve this answer

Anyway ...

If we supose $_POST is the author table you should do something like

$_POST['author'] = "authors"

In your code using ($_POST['author'] ? '' : '') is much better to use $_POST['author'] with table of authors.

And then select clausule should be like:

$table = $_POST["author"]; $DBH->prepare("SELECT * FROM books b INNER JOIN $table a ON b.author_id=a.id WHERE b.id = 1");

If we supose what you set in the post variable in the index you have to do:

$index = $_POST["author"]; $DBH->prepare("SELECT * FROM books WHERE author_id = $index");

It should work, but anyway, you should give us more information

tables for both option I thought are::

books is id, author id, ... author is id

share|improve this answer

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