Welcome Guest, Not a member yet? Register   Sign In
This empty string won't insert in my database

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 errors:

Quote: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


Quote: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 = (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));

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

More info:

If it's any help, the content of the empty strings I'm trying to insert is stdClass Object().

[eluser]Zack Kitzmiller[/eluser]
Do a print_r($t); and see the contents of $t.

This would be a great place to start debugging

Thanks techneke, this is returning

stdClass Object

for the in_reply_to_user_id field. I feel like I'm just not understanding how to convince PHP this is actually 'NULL,' or more precisely, I'm screwing something up.

Thanks again!

That's because it's not NULL. is_null on an empty object will return false. My suggestion would be to modify your code to check if it's an object as well:

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

This way if it's not null and is an object you set it as null, however if it's a string or integer it will return that string or integer.

jtkendall, brilliant! Thanks so much!

So essentially it's not NULL because there's an object there, the object just happens to be empty? And it only returns an empty object if there is no in_reply_to_user_id value set? Otherwise it returns a string? Er, string of integers?

Tiny necessary parenthesees-location modification for posterity's sake:

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

Theme © iAndrew 2016 - Forum software by © MyBB