Hi,
I wrote php code with jason format to access Twitter and show information on screen and store it to mysql.
The php code work fine without using insertion info. to DB.
If I try to store info. into DB, 50% of the time the info will be inserted completely and 50% of time I get error “Query Failed 1064” after few insertion query
Can you give me hint ?
[php]
<?php $localhost = 'localhost'; $dusername = 'root'; $dpassword = 'root'; $database = 'lab4'; $connection = new mysqli($localhost , $dusername , $dpassword,$database); if ($connection->connect_error) { die("Connection failed: " . $conn->connect_error); } //echo "Connected successfully"; require_once('TwitterAPIExchange.php'); /** Set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array( 'oauth_access_token' => "", 'oauth_access_token_secret' => "", 'consumer_key' => "", 'consumer_secret' => "" ); $url = "https://api.twitter.com/1.1/search/tweets.json"; $requestMethod = 'GET'; $getfield = '?q='.$_POST['search'].'&result_type=recent&count=50'; $twitter = new TwitterAPIExchange($settings); //$string = json_decode($twitter->setGetfield($getfield) //->buildOauth($url, $requestMethod) //->performRequest(),$assoc = TRUE); $twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); $tweets = json_decode($response); if ($message = isset($_GET['message']) ? $_GET['message']:'') { if($string["errors"][0]["message"] != "") {echo "Sorry, there was a problem.
Twitter returned the following error message:
".$string[errors][0]["message"]."
";exit();} } $i=1; foreach($tweets as $tweet) { foreach($tweet as $t) { echo $i; $profile_image_url = $t->user->profile_image_url; echo ''; $screen_name = $t->user->screen_name ; echo 'Name: '.$t->user->screen_name .'
'; $text = $t->text; echo 'Tweet: ' .$t->text . '.
'; $created_at = $t->created_at; echo 'Date of Tweet: ' .$t->created_at . '.
'; echo '
'; $i++; $quiry = 'INSERT INTO lab4 (profile_image_url,screen_name,text,created_at)'; // col in database $quiry .= "VALUES ('$profile_image_url','$screen_name','$text','$created_at')"; // variables that come from user which is decleare here $result = mysqli_query($connection, $quiry); if (!$result) { die("Query Faile". mysqli_errno($connection)); } } exit(); } echo "
"; print_r($string); echo ""; ?>
[/php]