Quantcast
Channel: Microsoft Drivers for PHP for SQL Server forum
Viewing all articles
Browse latest Browse all 391

bindValue problem with scandinavian characters using Sqlsrv 2.0 with PDO

$
0
0

I've made simple php-form to get data by city name...

<?php
$city = isset($_REQUEST["city"]) ? $_REQUEST["city"] : "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi" lang="fi">
  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>PDO Tester</title>
  </head>
  <body>
    <form action="" method="post">
      <label for="city">City</label>
      <input type="text" name="city" id="city" value="<?php echo htmlentities($city) ?>" />
      <input type="submit" value="Get" />
<?php
try 
{
  $server  = "xxx.xxx.xxx.xxx";
  $database = "mydb";
  $username = "myuid";
  $password = "mypwd";
  //$options = array( "CharacterSet" => "UTF-8");
  $connectionString = "sqlsrv:server=$server;Database=$database";
  $dbh = new PDO($connectionString, $username, $password);
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   
  try
  {
    $sql = "SELECT X, Y FROM MYTABLE WHERE CITY = :city";
    $stmt = $dbh->prepare($sql);
    $stmt->bindValue(":city", $city, PDO::PARAM_STR);

    if($stmt->execute())
    {
      $table = $stmt->fetchAll(PDO::FETCH_ASSOC);

      foreach($table as $row)
      {
        foreach($row as $fieldName => $fieldValue)
        {
          echo "<p>" . $fieldName . " = " . $fieldValue . "</p>";
        }
      }
    }
    else
    {
      print_r($stmt->errorInfo());
    }

    $stmt = null;
  }
  catch (PDOException $ex)
  {
    echo "Database::DataTable: " . $ex->getCode() . " - " . $ex->getMessage();
  }
}
catch ( PDOException $e ) 
{
  echo "An error occurred when creating database connection! <br>";
  die ( $e->getMessage() );
}
?>
    </form>
  </body>
</html>
It's working fine when city to search is for example 'Helsinki', but when city to search is for example 'Jyväskylä' or any city with scandinavian letters - it gives an error:

"Database::DataTable: IMSSP - SQLSTATE[IMSSP]: An error occurred translating string for input param 1 to UCS-2: No mapping for the Unicode character exists in the target multi-byte code page."

Database is SQL Server 2005

When I change (ie. "hard code")...

$stmt->bindValue(":city", "Jyväskylä", PDO::PARAM_STR);

...then it's working fine.

What wrong with my parameter binding with city names containin scandinavian letters?


Viewing all articles
Browse latest Browse all 391

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>