Jump to content

PDO Execute not working


herghost

Recommended Posts

Hi Guys

 

Any ideas why nothing is being stored in my database? I have echoed out all the variables and they appear fine, likewise I am not getting any PDO errors from the try/catch, just nothing is being stored!

 




<?PHP
//for ($x=0; $x <=9; $x++)
//{
// print_r($url);
// print_r("
");
// print_r("");
// print_r ($response);
// print_r("");
//}
$var = '@activePropertyCount';
foreach($response as $data)
{
$mycount = $data->HotelList->$var;

}

// while( $mycount > -1)
// {
$mycount = 1;
foreach($response as $data)
{
try {

$host = 'localhost';
$dbname = 'HotelWifi';
$user = 'removed';
$pass = 'removed';
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

}
catch(PDOException $e) {
echo $e->getMessage();
}
//print_r($data->HotelList->HotelSummary[$mycount-1]->hotelId);
//print_r($data->HotelList->HotelSummary[$mycount-1]);
$STH = $DBH->prepare("INSERT INTO HotelData (HotelID, HotelName, HotelCity, HotelCountry, LowRate, Long, Lat) values (:id,:name,:city,:country,:low,:long,:lat)");



$HotelID = $data->HotelList->HotelSummary[$mycount-1]->hotelId;
$HotelName = $data->HotelList->HotelSummary[$mycount-1]->name;
$HotelCity = $data->HotelList->HotelSummary[$mycount-1]->city;
$HotelCountry = $data->HotelList->HotelSummary[$mycount-1]->countryCode;
$LowRate = $data->HotelList->HotelSummary[$mycount-1]->lowRate;
$Long = $data->HotelList->HotelSummary[$mycount-1]->longitude;
$Lat = $data->HotelList->HotelSummary[$mycount-1]->latitude;


$STH->bindParam(":id", $HotelID);
$STH->bindParam(":name", $HotelName);
$STH->bindParam(":city", $HotelCity);
$STH->bindParam(":country", $HotelCountry);
$STH->bindParam(":low", $LowRate);
$STH->bindParam(":long", $Long);
$STH->bindParam(":lat", $Lat);

echo $HotelID . " " . $HotelName . " " . $HotelCity . " " . $HotelCountry . " " . $LowRate . " " . $Long . " " . $Lat;
try
{
$STH->execute();
print_r($STH);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}

//
//
//
//
//
// print_r($data->HotelList);
//
//
// $i = $i + 1;

?>


Link to comment
https://forums.phpfreaks.com/topic/286336-pdo-execute-not-working/
Share on other sites

  On 2/20/2014 at 2:34 AM, requinix said:

Can you post your real code? The full and complete version that can actually run?

Apologies, some of it appears to have been stripped, please see below:

<?PHP

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);



  //use latest minorRev 14
  $url  ='http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=14';
  $url .= 'removed';
  $url .= '&cid=55505';
  $url .= '&locale=en_US&city=Dallas&stateProvinceCode=TX&countryCode=US&numberOfResults=3';
  $url .= '&searchRadius=50';
//using the cache returns results much faster
  $url .= '&supplierCacheTolerance=MED_ENHANCED';
//dates and occupancy
  //$url .='&arrivalDate=19/02/2014&departureDate=09/05/2012&room1=2';
  $header[] = "Accept: application/json";
  $header[] = "Accept-Encoding: gzip";
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
  curl_setopt($ch,CURLOPT_ENCODING , "gzip");
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  $response = json_decode(curl_exec($ch));
 
    $var = '@activePropertyCount';
   //foreach($response as $data) 
//   {
//	$mycount = $data->HotelList->$var;
//	
//   }
   
  
	  $mycount = 1;
	    foreach($response as $data) 
   {
	   try {
  
 $host = 'localhost';
 $dbname = 'HotelWifi';
 $user = 'root';
 $pass = 'removed';
  # MySQL with PDO_MYSQL
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

}
catch(PDOException $e) {
    echo $e->getMessage();
}
	//print_r($data->HotelList->HotelSummary[$mycount-1]->hotelId);
	//print_r($data->HotelList->HotelSummary[$mycount-1]);
	$STH = $DBH->prepare("INSERT INTO HotelData (HotelID, HotelName, HotelCity, HotelCountry, LowRate, Long, Lat) values (:id,:name,:city,:country,:low,:long,:lat)");
	


$HotelID = $data->HotelList->HotelSummary[$mycount-1]->hotelId;
$HotelName = $data->HotelList->HotelSummary[$mycount-1]->name;
$HotelCity = $data->HotelList->HotelSummary[$mycount-1]->city;
$HotelCountry = $data->HotelList->HotelSummary[$mycount-1]->countryCode;
$LowRate = $data->HotelList->HotelSummary[$mycount-1]->lowRate;
$Long = $data->HotelList->HotelSummary[$mycount-1]->longitude;
$Lat = $data->HotelList->HotelSummary[$mycount-1]->latitude;


$STH->bindParam(":id", $HotelID);
$STH->bindParam(":name", $HotelName);
$STH->bindParam(":city", $HotelCity);
$STH->bindParam(":country", $HotelCountry);
$STH->bindParam(":low", $LowRate);
$STH->bindParam(":long", $Long);
$STH->bindParam(":lat", $Lat);

echo $HotelID . " " . $HotelName . " " . $HotelCity  . " " . $HotelCountry . " " . $LowRate . " " . $Long . " " . $Lat;
try
{
$STH->execute();
print_r($STH);
}
catch(PDOException $e) {
    echo $e->getMessage();
}
   }
	   
  
    ?>

  </body>
</html>

Mysql Reserved Words

Long is a reserved word, so your query is failing due to that error. You need to either quote this column name with backticks or change the name to something else that is not reserved, such as 'Lng' or 'Longitude'

 

 

 

  On 2/20/2014 at 2:31 AM, herghost said:

I am not getting any PDO errors from the try/catch

PDO: Errors and error handling

 

  Quote

PDO::ERRMODE_SILENT

 

This is the default mode. PDO will simply set the error code for you to inspect

You're not inspecting for an error code, as such you are not seeing the error. If you want exceptions, you need to tell PDO to use exceptions by setting the PDO::ATTR_ERRMODE attribute to PDO::ERRMODE_EXCEPTION.

 

You can set this attribute by either using the setAttribute method or by adding it to your parameters when constructing the PDO object, eg:

  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.