CodeIgniter Forums
This empty string won't insert in my database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: This empty string won't insert in my database (/showthread.php?tid=25270)



This empty string won't insert in my database - El Forum - 12-06-2009

[eluser]jpcody[/eluser]
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

and

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:

Code:
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().


This empty string won't insert in my database - El Forum - 12-06-2009

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

This would be a great place to start debugging


This empty string won't insert in my database - El Forum - 12-06-2009

[eluser]jpcody[/eluser]
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!


This empty string won't insert in my database - El Forum - 12-06-2009

[eluser]jtkendall[/eluser]
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:

Code:
$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.


This empty string won't insert in my database - El Forum - 12-06-2009

[eluser]jpcody[/eluser]
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);