PHP - Selecting Unicode data using NCHAR parameters
From Ingres Community Wiki
The following code uses query parameters to select to list all airports under the country code "ES" (Spain). When "ingres.utf8" is set to "true", all unicode character strings are assumed to be in UTF-8 form and will be coerced in the extension to UTF-16. When set to false all UNICODE strings must be provided in UTF-16 form. See the if (ini_get("ingres.utf8") block to see how the latter can be performed with PHP's iconv function.
<?php
/*
* @name : airport_search.php
* @author : grant.croker@ingres.com
*/
ini_set("ingres.utf8", TRUE);
$charset="UTF-8";
header('Content-Type: text/html; '.$charset);
$db = "demodb";
$user= "";
$password = "";
$link = ingres_connect($db, $user, $password);
$param_types = "nn";
$country_code = "ES%"; $town = "%";
$sql = "select ap_iatacode, ap_place, ap_name
from airport
where ap_ccode like ? and
ap_place like ? order by ap_iatacode";
if (ini_get("ingres.utf8")) {
$params["ccode"] = $country_code;
$params["area"] = $town;
} else {
echo "Converting to UTF-16LE from ISO-Latin-1<BR/>\n";
$params["ccode"] = iconv("ISO-8859-1", "UTF-16LE",$country_code);
$params["area"] = iconv("ISO-8859-1", "UTF-16LE", $town);
}
echo $country_code . " - " .bin2hex($country_code) . " - " .bin2hex($params["ccode"] ) . "\n\r";
echo $town . " - " .bin2hex($town) . " - " .bin2hex($params["area"] ) ."\n\r";
echo "executing '$sql'<br>\n";
$rc = ingres_query($link, $sql, $params, $param_types);
if ( ingres_stmt_errno() != 0 ) {
die (ingres_stmt_errno() . " " . ingres_stmt_error());
}
echo "fetching <br>\n";
echo "<table>\r\n";
while ($airport = ingres_fetch_object ($rc)) {
if (ini_get("ingres.utf8")) {
$ap_iatacode = $airport->ap_iatacode;
$ap_ccode = $airport->ap_place;
$ap_place = $airport->ap_name;
} else {
$ap_iatacode = iconv("UTF-16LE", "UTF-8", $airport->col1);
$ap_ccode = iconv("UTF-16LE", "UTF-8", $airport->col2);
$ap_place = iconv("UTF-16LE", "UTF-8", $airport->col3);
}
echo "<tr><td>".$ap_iatacode."</td><td>".$ap_ccode."</td><td>".$ap_place."</td></tr>\r\n";
}
echo "</table>\n\r";
ingres_commit($link);
ingres_close($link);
?>
The result will look something like:
ES% - 455325 - 455325 % - 25 - 25 executing 'select ap_iatacode, ap_place, ap_name from airport where ap_ccode like ? and ap_place like ? order by ap_iatacode'<br> fetching <br> <table> <tr><td>BCN</td><td>Barcelona</td><td>El Prat De Llobregat</td></tr> <tr><td>MAD</td><td>Madrid</td><td>Barajas</td></tr> <tr><td>VLL</td><td>Valladolid</td><td>Valladolid</td></tr> </table>
Back to PHP Driver Examples

