开发者

Help me debug my SQL INSERT, please?

开发者 https://www.devze.com 2023-02-15 01:39 出处:网络
I am pulling back some data from the twitter query API, and parsing it through PHP like so: $i =0; foreach ($tweetArray->results as $tweet) {

I am pulling back some data from the twitter query API, and parsing it through PHP like so:

    $i =0;
    foreach ($tweetArray->results as $tweet) {
        $timeStamp = strtotime($tweet->created_at开发者_JAVA技巧);
        $tweetDateTime = date('m-d-Y H:m:s', $timeStamp);
        if($i > 0){
            $SQL .= ',';
        }
        $SQL .= "(". $tweet->id .",'" . $tweet->from_user ."','". addslashes($tweet->profile_image_url) . "','". addslashes($tweet->text). "','" . $tweetDateTime ."')";
        $i++;
    }
    $SQL .= " ON DUPLICATE KEY UPDATE 1=1";

This leaves me with a SQL statement looking like this:

INSERT 
    INTO 
        tblTwitterSubmit (tweetId, twitterAuthor, authorAvatar, tweetText, tweetDateTime)
    VALUES
    (111,'name','http://url.com','a string of text','03-04-2011 13:03:09'),
    (222,'anothername','http://url.com','another tweet','03-04-2011 12:03:51')
ON DUPLICATE KEY UPDATE 1=1;

I am unfortunately getting the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1=1' at line 1.

Edit: The 1=1 is supposed to not do anything. The tweets don't change, and so if I pull the same one back twice for any reason, nothing will happen, but it also won't throw a duplicate key error.

Re-edit: The problem appears to have something to do with the key field I was using, which was the id of tweet as assigned by twitter.

I re-factored the code anyway, since it seemed pretty evident that what I had read in articles as a "really-good-idea" wasn't. I now included a PDO submit inside the for loop so I just make a bunch of submissions instead of one long sql string.

Hopefully this is better practice.

Leaving this open for a couple minutes hoping for some feedback if this is the way to do it or not.


The ON DUPLICATE KEY UPDATE requires a column name, something like this, assuming tweetId is the key column that's getting duplicates.

ON DUPLICATE KEY UPDATE tweetId=tweetId+1

Your 1=1 doesn't actually do anything.


Are you sure you're using the right syntax for on duplicate key update ?

Judging from it's manual's page, it seems you have to specify a column name, and not 1=1.


From what I understand, if you want to indicate "use the value from the values() clause when there's a duplicate", you should use something like this :

on duplicate key update your_col=values(your_col)

Quoting the relevant part :

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement.

In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred.

This function is especially useful in multiple-row inserts.


Then, as a sidenote, you must escape your strings using the function that matches your API -- probably mysql_real_escape_string -- and not the generic addslashes, which doesn't know about the specificities of your database engine.


The problem appears to have something to do with the key field I was using, which was the id of tweet as assigned by twitter.

I re-factored the code anyway, since it seemed pretty evident that what I had read in articles as a "really-good-idea" wasn't. I now included a PDO submit inside the for loop so I just

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号