>I create a charset=big5 collate=big5 table to store Chinese characters
>in big5 code. However, the INSERT sql failed when the character is
0x9f54. Why? If the value needs to be escaped, what function should I
use?
What database are you using? Different databases have different functions.
[chang@localhost ~]$ rpm -qa | grep sql
libdbi-dbd-mysql-0.7.1-3
sqlite-3.1.2-3
mysql-4.1.20-1.FC4.1
mysqlclient10-3.23.58-6
php-mysql-5.0.4-10.5
python-sqlite-1.1.6-1
postgresql-libs-8.0.8-1.FC4.1
mysql-devel-4.1.20-1.FC4.1
mysql-server-4.1.20-1.FC4.1
[chang@localhost ~]$ rpm -qa | grep php
php-ldap-5.0.4-10.5
php-gd-5.0.4-10.5
php-dbase-5.0.4-3.fc4
php-pear-5.0.4-10.5
php-mysql-5.0.4-10.5
php-5.0.4-10.5
--
iTech Consulting Co., Ltd.
Expert of ePOS solutions
Website:
http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
<?
class MYSQL {
private $fhandle;
var $row;
function MYSQL($host,$db,$usr,$pwd) {
$this->fhandle=mysqli_connect($host,$usr,$pwd,$db)
or die(mysqli_error());
$this->query("set names big5;");
$this->query("set character_set_client=big5;");
}
function query($sql_str) {
$this->row=mysqli_query($this->fhandle, $sql_str)
or die(mysqli_error($this->fhandle));
}
function affected_rows() {
return mysqli_affected_rows($this->row);
}
function num_rows() {
return mysqli_num_rows($this->row);
}
function fetch_assoc() {
return mysqli_fetch_assoc($this->row);
}
function __destruct() {
mysqli_close($this->fhandle);
}
function begin_tran() {
mysqli_autocommit($this->fhandle, FALSE);
}
function commit() {
mysqli_commit($this->fhandle);
mysqli_autocommit($this->fhandle, TRUE);
}
function rollback() {
mysqli_rollback($this->fhandle);
mysqli_autocommit($this->fhandle, TRUE);
}
}
function showcode($cChar) {
return "[".ord(substr($cChar,0,1)).".".ord(substr($cChar,1, 1))."]";
}
$target=new MYSQL("localhost","order_dev","root","123456");
$fhandle=dbase_open("canton.DBF",0);
if ($fhandle) {
$reccount=dbase_numrecords($fhandle);
echo "input count: ".$reccount."\n";
# clear table
$target->query("drop table canton");
$target->query(
"create table canton ("
. " big5 char(2) collate big5_bin not null,"
. " unicode char(20) not null,"
. " thekey char(6) collate big5_bin ,"
. " canton char(10) collate big5_bin ,"
. " changjei char(10) collate big5_bin,"
. " touched integer collate big5_bin,"
. " primary key (big5)"
. " ) default charset=big5 collate=big5_bin;"
);
for ($ii=1; $ii<=$reccount; $ii++) {
$row=dbase_get_record_with_names($fhandle,$ii);
$ss=$row['BIG5'];
$tt="0x".dechex(ord(substr($ss,0,1))).dechex(ord(s ubstr($ss,1,1)));
echo $ss.$tt."\n";
$target->query("select * from canton where big5=".$tt."");
$yy = $target->num_rows();
if ($yy>0) {
$query="update canton set touched="
. $row["TOUCHED"]
. " where big5=" . $tt . "";
}
else {
$query="insert into canton ("
. " big5,"
. " unicode,"
. " thekey,"
. " changjei,"
. " canton,"
. " touched "
. ") values ("
. "".$tt.","
. "'mb_convert_encoding',"
. "'".$row["THEKEY"]."',"
. "'".$row["CHANGJEI"]."',"
. "'".$row["CANTON"]."',"
. $row["TOUCHED"]
. ")";
}
$result=$target->query($query);
}
$result=$target->query("select count(*) as cnt from canton");
$yy = $target->fetch_assoc();
echo "output count: ".$yy['cnt']."\n";
dbase_close($fhandle);
}
?>