note 49852 deleted from function.mysql-connect by bjori
| From: | bjori@php.net | Date: | Sat, 15 Apr 2006 18:32:45 +0000 |
| Subject: | note 49852 deleted from function.mysql-connect by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108180@lists.php.net to get a copy of this message | ||
Note Submitter: k dot gornik at xds dot pl
----
To avoid something like this:
Database error: Invalid SQL: SELECT * FROM alias WHERE path='1108043672' AND
fk_client_id=2
MySQL Error: 1267 (Illegal mix of collations (latin2_general_ci,IMPLICIT) and
(latin1_swedish_ci,COERCIBLE) for operation '=')
Session halted.
PHP >=4.3.10
MySQL >=4.1.0
simply write:
<?php
class MySQL {
//[...]
function connect($host, $user, $pass) {
$this->Link_DB = @mysql_connect($host, $user, $pass);
if($this->Link_DB) {
if(version_compare(phpversion(), "4.3.10", ">=")>0) {
$result = @mysql_query("SHOW VARIABLES LIKE 'version'", $this->Link_ID);
$row = mysql_fetch_row($result);
$mysql_ver = $row[1];
if(version_compare($mysql_ver, '4.2.0', '>=')>0) {
$i = 1;
while(defined('DB_INIT_CODE_'.$i)) {
$sQuery = constant('DB_INIT_CODE_'.$i);
mysql_query($sQuery, $this->Link_ID);
$i++;
}
}
}
}
else {
return false;
}
return true;
}
}
?>
and
<?php
define('DB_INIT_CODE_1', "SET NAMES latin2");
define('DB_INIT_CODE_2', "SET CHARACTER SET latin2");
define('DB_INIT_CODE_3', "SET
COLLATION_CONNECTION='latin2_general_ci'");
// and so on...
?>
elsewhere (like I put it down in MySQL.config.inc.php)
The only important thing is You MUST include definitions before including object MySQL :)
Spent lot of time looking for prettier solution but there seems not to be any...