IF EXISTS UPDATE ELSE INSERT 的语法错误

Syntax error with IF EXISTS UPDATE ELSE INSERT(IF EXISTS UPDATE ELSE INSERT 的语法错误)
本文介绍了IF EXISTS UPDATE ELSE INSERT 的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用托管在我的 ISP 上的 MySQL 5.1.这是我的查询

I'm using MySQL 5.1 hosted at my ISP. This is my query

mysql_query("
IF EXISTS(SELECT * FROM licensing_active WHERE title_1='$title_1') THEN
    BEGIN
        UPDATE licensing_active SET time='$time' WHERE title_1='$title_1')
    END ELSE BEGIN
        INSERT INTO licensing_active(title_1) VALUES('$title_1')
    END   
") or die(mysql_error());  

错误是

... check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS(SELECT * FROM licensing_active WHERE title_1='Title1') THEN ' at line 1

我的实际任务涉及

WHERE title_1='$title_1' AND title_2='$title_2' AND version='$version' ...ETC...

但我已经减少了它以使解决问题的事情变得更简单

but I have reduced it down to make things simpler for my problem solving

在我的搜索中,我不断看到对ON DUPLICATE KEY UPDATE"的引用,但不知道该怎么做.

In my searches on this, I keep seeing references to 'ON DUPLICATE KEY UPDATE', but don't know what to do with that.

推荐答案

这里有一个简单易行的解决方案,试试看.

Here is a simple and easy solution, try it.

$result = mysql_query("SELECT * FROM licensing_active WHERE title_1 ='$title_1' ");

if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE licensing_active SET time = '$time' WHERE title_1 = '$title_1' ");
}
else
{
    mysql_query("INSERT INTO licensing_active (title_1) VALUES ('$title_1') ");
}

注意:虽然这个问题来自 2012 年,但请记住,mysql_* 函数自 PHP 7 起不再可用.

Note: Though this question is from 2012, keep in mind that mysql_* functions are no longer available since PHP 7.

这篇关于IF EXISTS UPDATE ELSE INSERT 的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)