views:

84

answers:

6

Forgive me if I could have any sort of fundamental error here. I'd imagine there's something simple I'm missing. I'm looking to store Twitter updates in a database with only a few fields: an auto-increment index, the time posted, the actual status update & the user id the update is in reply to.

I'm simply storing this last field so I can provide a method of filtering out replies.

But it appears that my SQL code is throwing an error. As of this writing, the SQL properly inserts the two most recent updates, which are both replies to another user and, therefore, have data in the in_reply_to_user_id field. But on the third update, which is not in reply to anyone, I get the following error:

Error Number: 1064

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 ')' at line 1

INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (1260070319, 'I guess Johnny Cash knew what he was talking about in that \"A Boy Named Suh\" song. That guy is both fast and mean.', '6389320556', )

Twitter's API states no default value for this parameter. I tried the same query with the "favorited" parameter, and it correctly labeled each row with "false" in my database. So I'm assuming my problem is with inserting an empty string.

For what it's worth, here is my CodeIgniter method:

function insert_tweet($tweet){
 foreach($tweet as $t) {
  $when = strtotime($t->created_at);
  $status = $t->text;
  $postid = $t->id;
  $reply = $t->in_reply_to_user_id;
$sql = 'INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (?, ?, ?, ?)';
  $this->db->query($sql, array($when, $status, $postid, $reply));
 }
}

Any help you could give would be great! I hope I've provided enough information!

More info: I should also note that CodeIgniter is throwing the following error:

A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: database/DB_driver.php

Line Number: 598

In the off chance that this proves some sort of CI idiosyncrasy to be at fault.

A: 

I don't know php but in asp.net style pseudo code i would do this...

$this->db->query($sql, array($when, $status, $postid, string.IsNullOrEmpty($reply) ? null : $reply));

Hopefully you can adapt, just need to deal with the empty parameter.

Paul Creasey
Thanks, Paul. I think this is essentially what Binary Worrier and Nighthawk have suggested in PHP with the ternary operator. I think your suggestion emphasizes that this *is* the correct solution, now I just have to figure out how to wrap my brain around the implementation. Thanks again!
Joshua Cody
A: 

I'm not sure about codeigniter (never used it) but I know that you will need to get the null value to appear as NULL

function insert_tweet($tweet){
    foreach($tweet as $t) {
            $when = strtotime($t->created_at);
            $status = $t->text;
            $postid = $t->id;
            $reply = (is_null($t->in_reply_to_user_id) ? 'NULL' : $t->in_reply_to_user_id);
            $sql = 'INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (?, ?, ?, ?)';
            $this->db->query($sql, array($when, $status, $postid, $reply));
    }

}

I'm not sure if that will produce the desired SQL:

INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (1260070319, 'I guess Johnny Cash knew what he was talking about in that \"A Boy Named Suh\" song. That guy is both fast and mean.', '6389320556', NULL)

edit Also make sure that the field in the database can be null.

Hope this helps..

NiGhTHawK
As of now, this *seems* as if it would work, but no cigar. I'm getting the same 'ole, same 'ole.I've tried text, varchar and int, each set to both "null" and "not null," but to no avail.More ideas are certainly welcomed.
Joshua Cody
A: 

I don't speak codeigniter, but I can tell you how to fix your problem.
You have two options

  1. Check is the string empty, if so, instead of having a blank value, put in the null keyword.

  2. Check is the value empty, if so, leave that column out of the insert statement's cloumns and values.

Hope this helps

Binary Worrier
Binary Worrier, thanks so much. I feel like this solution is right, but I think I'm having trouble in my implementation. Am I misguided with this: $this->db->query($sql, array($when, $status, $postid, if(empty($reply)){'NULL'}else{$reply}));
Joshua Cody
And additionally, in phpmyadmin, should I set my column to "null" instead of "not null"—I tried both ways to no avail.
Joshua Cody
A: 

Why not alter the last column and make it default to NULL?

When you want to insert some record without this field you just ignore it in the 'INSERT' statement.

And be sure not to put a pending comma between the last column value and the right parenthisis. :)

Satoru.Logic
This solution seemed easiest, so I tried it first. When setting the column to "null" and adding a default value of NULL via phpmyadmin, I got the same errors.And I believe the last column value only has a period due to the fact that there is no string for my fourth parameter.Let me know if I misunderstood you at all, and thanks for your help!
Joshua Cody
A: 

You're coming from the CodeIgniter forums right? I think the issue is coming from your Twitter library. Can you post that code? For somereason, it's returning a NULL status as an Object, which it shouldn't do.

Zack
Hey Zack, I am coming from the CodeIgniter forums, and I ended up with a great solution over there. About to post it here for the world to enjoy.
Joshua Cody
A: 

Hey guys, the answers here seemed to be close, but no cigar. I got some help from the great folks on the CodeIgniter forums, and here's what I came up with:

$reply = (is_null($t->in_reply_to_user_id) || is_object($t->in_reply_to_user_id) ? ‘NULL’ : $t->in_reply_to_user_id);

Essentially, like Zack said, it is returning the empty field from the Twitter API as an empty object instead of null. So I needed to check if this is either null or an object, then return NULL. And if not, return the user_id string.

Thanks a ton for your help!

Joshua Cody
Honestly, I can see that that _works_ but it hardly seems like the correct solution. I'd like to have a look at your Twitter Lib.
Zack
Hey Zack, I'm using the following library: http://github.com/simonmaddox/codeigniter-twitterI haven't made any additions or subtractions within the function I'm using, and anything else I've done is minimal.
Joshua Cody