1

I'm trying to execute the following SQL query

$tweet_id_int = intval($tweet->id);
$result = db_query("INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES     ('%v1','%v2','%v3','%v4')", array('%v1'=>$twitter_handle,'%v2'=>$tweet->text,'%v3'=>$tweet->created_at,'%v4'=> $tweet_id_int));

from inside a Drupal module and getting the error

    PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '%v4' for column 'TweetID' at row 1: INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES ('%v1','%v2','%v3','%v4'); Array ( [%v1] => davecohen [%v2] => taking it easy - Rosh Hashana [%v3] => Fri Sep 14 08:06:08 +0000 2007 [%v4] => 267918272 ) 

handle, tweet and time_string are VARCHAR

tweetid is BIGINT

The "TweetID" is the value I received from calling the Twitter API earlier to fetch some tweets. Twitter API returns the Tweet ID as a string, so I called 'intval' before passing it to the SQL query. If I try running the same query into the MySQL database directly via the Sequel Pro console

 INSERT INTO tweetsDB (handle,tweet,time_string,tweetid) VALUES ('davecohen','taking it easy - Rosh Hashana','Fri Sep 14 08:06:08 +0000 2007','267918272')

Then I get no problems at all.

Any help here would be MUCH appreciated!

1

From the Drupal API on db_query:

Do not use this function for INSERT, UPDATE, or DELETE queries. Those should be handled via db_insert(), db_update() and db_delete() respectively.

The recommended method:

// Insert values returned from Twitter API.
$tweetsdb_id = db_insert('tweetsDB')
->fields(array(
  'handle' => $handle,
  'tweet' => $tweet,
  'time_string' =>$time_string,
  'tweetid' => $id,
))
->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.