0

I am trying to make a single query with SQL conditions like the above:

Let's say first that we have the PHP variable $reco that will let us update the data when we want:

mysql_query("
INSERT INTO app_table (views, average)
VALUES
(
IF($reco = 'yes', '$views', views),
IF($reco = 'yes', '$aver', average)
)
ON DUPLICATE KEY UPDATE
views = IF($reco = 'yes', $views, views),
average = IF($reco = 'yes', $aver, average)
");

The problem is that $reco doesn't works as I want inside the query but a column of table will work, so how can make it work with a PHP variable?

For example if I had reco inside the table would work like this:

Notice that I have the $reco in php variable not inside the table.

mysql_query("
INSERT INTO app_table (views, average)
VALUES
(
IF(reco = 'yes', '$views', views),
IF(reco = 'yes', '$aver', average)
)
ON DUPLICATE KEY UPDATE
views = IF(reco = 'yes', $views, views),
average = IF(reco = 'yes', $aver, average)
");
5
  • It's really hard to tell what the problem and/or question is. And that is besides the fact I tried to find the above query. Commented Nov 14, 2013 at 21:39
  • what's in $reco? If it's something like blahblahblah, and you don't have a blahblahblah field in your table, then the query is going to blow up on you. mysql_query(...) or die(mysql_error()) will tell you exactly WHERE/how the query's failing. That or you're totally not understanding how PHP and SQL operate... Commented Nov 14, 2013 at 21:40
  • I don't understand the question either Commented Nov 14, 2013 at 21:41
  • start with seperating the php and mysql Commented Nov 14, 2013 at 21:42
  • Guys, the $reco is 'yes' or 'no' php variable string and i am saying for example in "IF($reco = 'yes', '$views', views)" if $reco is 'yes' make views = '$views' else leave views as views ($views -> php, views -> sql colum) Commented Nov 14, 2013 at 21:43

2 Answers 2

3

Well, if it's kind of a proof of the principle, and you still want to do it on the SQL side:

$query = "... IF('$reco' = 'yes', '$views' ...";

Note the quotes around $reco. So, if $reco were 'yes' the query would be interpreted as ... IF ('yes' = 'yes', 'some_value' ...

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

5 Comments

har-wradim many thanks, it will be a pure string inside the sql query, i am searching this for hours and my mind don't went there! Thanks!
Don't forget to up-vote the answer and/or mark it as the accepted one, if you like :). I want to stress it again: it's just a general principle useful in some situations, but I wouldn't do it this way in your particular case (if I understand it correctly).
I can not vote because i dont have enough reputation, but i have another question, how to make if condition to include limit or not? like: $query = "SELECT **** FROM **** WHERE **** GROUP BY **** ORDER BY **** DESC IF(some_condition) LIMIT ***** END IF;
It's not straightforward, but you can try: stackoverflow.com/questions/7899639
Thanks i found a solution to do this with IF(A,B,C)
0

The easiest way, I think, is to do your string comparison purely in PHP:

if ($reco == 'yes') {
   $views = $views_value;
   $average = $average_value;
}
else {
   $views = $views_default;
   $average = $average_default;
}

//escape your $views and $average strings, if necessary
$query = "INSERT INTO app_table (views, average) VALUES ('$views', '$average') ON DUPLICATE KEY UPDATE //whatever";

3 Comments

Thanks har-wradim this was my original code and dont forget to get views and average inside else statement i need a select query so the code becomes bigger but i am trying to short it as i am changing the code often.
Actually this way you perform string comparison only once. If you do it the way you proposed: it's done 4 times (on the SQL side)
Yes i know, but i wanna do it as i want short code and this way i am escaping from make a Select query to get views or average. Actualy the query is more more bigger and this way it will be shorten

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.