2016-10-31 1 views
3

のMySQLとは異なりますするmysql_query()の挙動は、私は次の表を持って、コマンドラインで

INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1; 
INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1; 

...これがあります出力は、私が取得:

+----+--------+------+-------+---------------+-------+ 
| id | closed | user | level | comment  | count | 
+----+--------+------+-------+---------------+-------+ 
| 9 |  0 | 1 | 50 | First insert |  1 | 
| 10 |  0 | 1 | 75 | Second insert |  1 | 
+----+--------+------+-------+---------------+-------+ 

私はmysql_query()を使用してこれらのコマンドを実行すると、これは私が得るものです。

+----+--------+------+-------+---------------+-------+ 
| id | closed | user | level | comment  | count | 
+----+--------+------+-------+---------------+-------+ 
| 11 |  0 | 1 | 50 | Second insert |  2 | 
+----+--------+------+-------+---------------+-------+ 

ので、クエリが更新の代わりに、私はmysql_query()機能を使用する場合、それはすべきではないときに、2つのインサートが異なるレベルを持っているので、彼らは一意であるため、右...、新しい行を挿入していますか?間違っているのですか、ここに何か起こっていますか?

編集:私はそれは使い正確にコードスニペット:

char* query = "INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;"; 
char* query2 = "INSERT INTO db.insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;"; 
mysql_query(link, query); 
mysql_query(link, query2); 

私はリンクが正しく、それが仕事をしているため、このコードは以外(つまり、クエリが実行されている)で実行されていることを知っています挿入するのではなく更新する箇所を発行してください。

+5

mysql_query()を実行する場所でコードスニペットを共有できますか? – dchayka

+2

**警告**:PHPを学んでいるだけの方は、[mysql_query'](http://php.net/manual/en/function.mysql-query.php)インタフェースを使用しないでください。それはPHP 7で削除されたのでとてもひどいと危険です。[PDOのようなものは学ぶのが難しくない](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)と[PHP The Right Way](http://www.phptherightway.com/)のようなガイドがベストプラクティスを説明しています。 – tadman

+1

私は上記のコードスニペットを入れて、もっと必要な場合にお知らせください。そして、私は言及すべきでした、これはCでPHPではありません。 – user2073805

答えて

1

問題は再現できません。私はちょうど上記のあなたのテーブル(簡単なテストケースを作ってくれてありがとう)とクイックCプログラムでこれをテストしました。期待どおりに動作し、2行挿入しました。 MySQL 8.0.0-dmrの使用

#include <my_global.h> 
#include <mysql.h> 
#include <string.h> 

int main(int argc, char **argv) 
{ 
    MYSQL *con = mysql_init(NULL); 

    if (con == NULL) 
    { 
     fprintf(stderr, "%s\n", mysql_error(con)); 
     exit(1); 
    } 

    mysql_real_connect(con, "localhost", "root", "password", "test", 0, NULL, 0); 

    if (strlen(mysql_error(con))) 
    { 
     fprintf(stderr, "%s\n", mysql_error(con)); 
     mysql_close(con); 
     exit(1); 
    } 

    char* query = "INSERT INTO insert_test (closed, user, level, comment, count) VALUES (0, 1, 50, 'First insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;"; 
    char* query2 = "INSERT INTO insert_test (closed, user, level, comment, count) VALUES (0, 1, 75, 'Second insert', 1) ON DUPLICATE KEY UPDATE comment=VALUES(comment), count=count+1;"; 

    mysql_query(con, query); 

    if (mysql_errno(con)) 
    { 
     fprintf(stderr, "Error: %s\n", mysql_error(con)); 
     mysql_close(con); 
     exit(1); 
    } 

    printf("Rows: %ld\n", (long) mysql_affected_rows(con)); 

    if (mysql_warning_count(con)) 
    { 
     fprintf(stderr, "Warnings: %s\n", mysql_error(con)); 
     mysql_close(con); 
     exit(1); 
    } 

    mysql_query(con, query2); 

    if (mysql_errno(con)) 
    { 
     fprintf(stderr, "Error: %s\n", mysql_error(con)); 
     mysql_close(con); 
     exit(1); 
    } 

    printf("Rows: %ld\n", (long) mysql_affected_rows(con)); 

    if (mysql_warning_count(con)) 
    { 
     fprintf(stderr, "Warnings: %s\n", mysql_error(con)); 
     mysql_close(con); 
     exit(1); 
    } 

    mysql_close(con); 
    exit(0); 
} 

出力が示す:表の

Rows: 1 
Rows: 1 

データ:

mysql> select * from insert_test; 
+----+--------+------+-------+---------------+-------+ 
| id | closed | user | level | comment  | count | 
+----+--------+------+-------+---------------+-------+ 
| 5 |  0 | 1 | 50 | First insert |  1 | 
| 6 |  0 | 1 | 75 | Second insert |  1 | 
+----+--------+------+-------+---------------+-------+ 

プログラムを複数回実行すると、両方の行のcountカラムをインクリメントします。これが起こると、各文に影響を受けた2行が表示されます。これは正常です。

mysql> select * from insert_test; 
+----+--------+------+-------+---------------+-------+ 
| id | closed | user | level | comment  | count | 
+----+--------+------+-------+---------------+-------+ 
| 23 |  0 | 1 | 50 | First insert |  2 | 
| 24 |  0 | 1 | 75 | Second insert |  2 | 
+----+--------+------+-------+---------------+-------+ 
関連する問題