Query Failed 1064

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]

If you were using prepared statements/parameterized queries, it wouldn’t be failing. When you grab text that has a single apostrophe, your syntax is incorrect.

Thank you very much

I fixed it and it work perfectly so far

I used mysql_real_escape_string() method

Thank you I appropriate it

Sponsor our Newsletter | Privacy Policy | Terms of Service