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)
");
$reco? If it's something likeblahblahblah, and you don't have ablahblahblahfield 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...