0

Need some help debugging the following json call to twitter to store result in mysql. Call is working fine, but seem to have an error writing to the db. Any assistance is much appreciated

<?php

$q = $_GET["q"]; // query string
$request = "http://search.twitter.com/search.json?q=".urlencode($q);
$response = file_get_contents($request);
$jsonobj = json_decode($response);

{
 echo( $response );
 }


if($jsonobj != null){

    $con = mysql_connect('host', 'admin', 'pw');

    if (!$con){
        die('Could not connect: ' . mysql_error());
    }

    foreach($jsonobj->results as $item){

        $id = $item->id;
        $created_at = $item->created_at;
        $created_at = strtotime($created_at);
        $mysqldate = date('Y-m-d H:i:s',$created_at);
        $from_user = mysql_real_escape_string($item->from_user);
        $from_user_id = $item->from_user_id;
        $text = mysql_real_escape_string($item->text);
        $source = mysql_real_escape_string($item->source);
        $geo = $item->geo;
        $iso_language_code = $item->iso_language_code;
        $profile_image_url = mysql_real_escape_string($item->profile_image_url);
        $to_user_id = $item->to_user_id;
        if($to_user_id==""){ $to_user_id = 0; }
        $query = mysql_real_escape_string($query);

        mysql_select_db("database", $con);
        $query = "INSERT into tweets VALUES ($id,'$mysqldate','$from_user',$from_user_id,'$text','$source','$geo','$iso_language_code','$profile_image_url',$to_user_id,'$q')";
        $result = mysql_query($query);

    }

    mysql_close($con);
}

?>
WAUS
  • 63
  • 7
  • You are not doing any error checking in your query, so it's no wonder if things break silently. How to properly check errors is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Feb 04 '12 at 14:54
  • firstly, `$id`, `$to_user_id` and `$from_user_id` is not in quotes in the query. –  Feb 04 '12 at 14:58
  • You also state what to insert, but not which columns to insert them in....Do some googling on "SQL insert query" –  Feb 04 '12 at 15:00

0 Answers0