0

I have this code:

$post = 0;
$rows = $wpdb->get_results( "select * from table where if('".$post."' != 0, id = '".$post."', id != 0) " );

If $post doesn't have value 0, I need to take all rows which have id $post, and if $post has id 0, I need to take all rows which don't have id 0, but it doesn't return any result

1
  • you will need ternary there... Commented Jan 6, 2014 at 12:00

2 Answers 2

2

If statements can't be used to make parts of the query dynamic try this instead:

"select * from table where " . ($post ? "id = $post" : "id != 0")

This assumes that $post is not user supplied. If it is you will have to ensure it's a number and not malicious.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of doing :

$post = 0;

$rows = $wpdb->get_results( "select * from table where if('".$post."' != 0, id = '".$post."', id != 0) " );

First try to do in this way:

$sql = "select * from table where if('".$post."' != 0, id = '".$post."', id != 0)";

echo "$sql";

copy the out put and try to run it in your actual database manually. If it will show the result then it will work in your wp code also.

Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.