当前位置: 编程技术>php
本页文章导读:
▪56.com视频采集接口程序(PHP)
楼下也有一个所谓56站程序的,忽悠到了极致,不喜欢被人忽悠的,还是自己做个站吧! 我给大家个采集接口,你爱自己采集可以用自己的FLASH播放器直接播放56.com的flv吧!不用帮人家做免.........
▪从MySQL数据库表中取出随机数据的代码
MySQL 如何从表中取出随机数据 以前在群里讨论过这个问题,比较的有意思.mysql的语法真好玩. 他们原来都想用PHP的实现随机,但取出多条好像要进行两次以上查询. 翻了手册,找到了下面这个.........
▪小结下MySQL中文乱码,phpmyadmin乱码,php乱码 产生原因及其解决方法第1/3页
乱码产生原因 mysql字符编码是版本4.1引入的,支持多国语言,而且一些特性已经超过了其他的数据库系统。 我们可以在MySQL Command Line Client 下输入如下命令查看mysql的字符集 mysql> SHOW CH.........
[1]56.com视频采集接口程序(PHP)
来源: 互联网 发布时间: 2013-11-30
楼下也有一个所谓56站程序的,忽悠到了极致,不喜欢被人忽悠的,还是自己做个站吧!
我给大家个采集接口,你爱自己采集可以用自己的FLASH播放器直接播放56.com的flv吧!不用帮人家做免费广告那么傻了!
<?php
$url = "http://www.56.com/u75/v_MTkwMTIwMjQ.html";
$html = @file_get_contents($url);
preg_match_all("/var _oFlv_o = '([\S|\s]+)'/isU", $html, $matches);
$o = decode56($matches[1][0]);
include_once "json.php"; //当php没有配置支持json的时候有用
$o = str_replace("'", '"', $o);
$o = json_decode($o);
//print_r($o);
$swf = "http://www.56.com/n_" . str_replace(":88", "", str_replace(".56.com", "", $o->img_host)) .
"_/" . str_replace(".56.com", "", $o->URL_host) .
"_/" . $o->URL_pURL .
"_/" . $o->URL_sURL .
"_/" . $o->user_id .
"_/" . $o->URL_URLid .
"_/" . $o->totaltime .
"_/" . $o->effectID .
"_/" . $o->id . ".swf";
$flv = "http://hot-chinacache.56.com/flvdownload/" . $o->URL_pURL .
"/" . $o->URL_sURL .
"/" . $o->user_id .
"@56.com_56flv_" . $o->URL_URLid . ".flv";
echo "FLASH文件路径: " . $swf;
echo "<br>";
echo "FLV文件路径: " . $flv;
$de = 0;
function decode56($str) {
global $de;
$c1 = "";
$c2 = "";
$c3 = "";
$c4 = "";
$c5 = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);
$c6 = charCodeAt($str, 0);
$c7 = "";
$i = "";
$len = "";
$out = "";
if(!$de) {
$de = 1;
$c7 = decode56($str);
$str = substr($c7, $c6);
}
$len = strlen($str);
$i = 1;
$out = "";
while ($i < $len) {
do {
$c1 = $c5[charCodeAt($str, $i++) & 0xff];
}
while($i < $len && $c1 == -1);
if ($c1 == -1) break;
do {
$c2 = $c5[charCodeAt($str, $i++) & 0xff];
}
while ($i < $len && $c2 == -1);
if($c2 == -1) break;
$out .= chr(($c1 << 2) | (($c2 & 0x30) >> 4));
do {
$c3 = charCodeAt($str, $i++) & 0xff;
if($c3 == 61) return $out;
$c3 = $c5[$c3];
}
while ($i < $len && $c3 == -1);
if($c3 == -1) break;
$out .= chr((($c2 & 0XF) << 4) | (($c3 & 0x3C) >> 2));
do {
$c4 = charCodeAt($str, $i++) & 0xff;
if($c4 == 61) return $out;
$c4 = $c5[$c4];
}
while ($i < $len && $c4 == -1);
if ($c4 == -1) break;
$out .= chr((($c3 & 0x03) << 6) | $c4);
}
return $out;
}
function charCodeAt($str, $i){
return ord(substr($str, $i, 1));
}
//该程序仅为技术交流,交流可加QQ群: 1714987
?>
json.php
<?php
define('HTML_AJAX_JSON_SLICE', 1);
define('HTML_AJAX_JSON_IN_STR', 2);
define('HTML_AJAX_JSON_IN_ARR', 4);
define('HTML_AJAX_JSON_IN_OBJ', 8);
define('HTML_AJAX_JSON_IN_CMT', 16);
define('HTML_AJAX_JSON_LOOSE_TYPE', 10);
define('HTML_AJAX_JSON_STRICT_TYPE', 11);
class HTML_AJAX_JSON
{
function HTML_AJAX_JSON($use=HTML_AJAX_JSON_STRICT_TYPE)
{
$this->use = $use;
}
function encode($var)
{
switch(gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false';
case 'NULL':
return 'null';
case 'integer':
return sprintf('%d', $var);
case 'double':
case 'float':
return sprintf('%f', $var);
case 'string': // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
$ascii = '';
$strlen_var = strlen($var);
for($c = 0; $c < $strlen_var; $c++) {
$ord_var_c = ord($var{$c});
if($ord_var_c == 0x08) {
$ascii .= '\b';
} elseif($ord_var_c == 0x09) {
$ascii .= '\t';
} elseif($ord_var_c == 0x0A) {
$ascii .= '\n';
} elseif($ord_var_c == 0x0C) {
$ascii .= '\f';
} elseif($ord_var_c == 0x0D) {
$ascii .= '\r';
} elseif(($ord_var_c == 0x22) || ($ord_var_c == 0x2F) || ($ord_var_c == 0x5C)) {
$ascii .= '\\'.$var{$c}; // double quote, slash, slosh
} elseif(($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)) {
// characters U-00000000 - U-0000007F (same as ASCII)
$ascii .= $var{$c}; // most normal ASCII chars
} elseif(($ord_var_c & 0xE0) == 0xC0) {
// characters U-00000080 - U-000007FF, mask 110XXXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1})); $c+=1;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xF0) == 0xE0) {
// characters U-00000800 - U-0000FFFF, mask 1110XXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2})); $c+=2;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xF8) == 0xF0) {
// characters U-00010000 - U-001FFFFF, mask 11110XXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3})); $c+=3;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xFC) == 0xF8) {
// characters U-00200000 - U-03FFFFFF, mask 111110XX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4})); $c+=4;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xFE) == 0xFC) {
// characters U-04000000 - U-7FFFFFFF, mask 1111110X, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4}), ord($var{$c+5})); $c+=5;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
}
}
return sprintf('"%s"', $ascii);
case 'array':
if(is_array($var) && (array_keys($var) !== range(0, sizeof($var) - 1)))
return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($var), array_values($var))));
return sprintf('[%s]', join(',', array_map(array($this, 'encode'), $var)));
case 'object':
$vars = get_object_vars($var);
return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($vars), array_values($vars))));
default:
return '';
}
}
function name_value($name, $value)
{
return (sprintf("%s:%s", $this->encode(strval($name)), $this->encode($value)));
}
function reduce_string($str)
{
$str = preg_replace('#^\s*//(.+)$#m', '', $str); // eliminate single line comments in '// ...' form
$str = preg_replace('#^\s*/\*(.+)\*/#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at start of string
$str = preg_replace('#/\*(.+)\*/\s*$#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at end of string
$str = trim($str); // eliminate extraneous space
return $str;
}
function decode($str)
{
$str = $this->reduce_string($str);
switch(strtolower($str)) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
if(is_numeric($str)) {
return ((float)$str == (integer)$str)
? (integer)$str
: (float)$str;
} elseif(preg_match('/^".+"$/s', $str) || preg_match('/^\'.+\'$/s', $str)) {
$delim = substr($str, 0, 1);
$chrs = substr($str, 1, -1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
for($c = 0; $c < $strlen_chrs; $c++) {
$substr_chrs_c_2 = substr($chrs, $c, 2);
$ord_chrs_c = ord($chrs{$c});
if($substr_chrs_c_2 == '\b') {
$utf8 .= chr(0x08); $c+=1;
} elseif($substr_chrs_c_2 == '\t') {
$utf8 .= chr(0x09); $c+=1;
} elseif($substr_chrs_c_2 == '\n') {
$utf8 .= chr(0x0A); $c+=1;
} elseif($substr_chrs_c_2 == '\f') {
$utf8 .= chr(0x0C); $c+=1;
} elseif($substr_chrs_c_2 == '\r') {
$utf8 .= chr(0x0D); $c+=1;
} elseif(($delim == '"') && (($substr_chrs_c_2 == '\\"') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
$utf8 .= $chrs{++$c};
} elseif(($delim == "'") && (($substr_chrs_c_2 == '\\\'') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
$utf8 .= $chrs{++$c};
} elseif(preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6))) { // single, escaped unicode character
$utf16 = chr(hexdec(substr($chrs, ($c+2), 2))) . chr(hexdec(substr($chrs, ($c+4), 2)));
$utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'utf-8');
$c+=5;
} elseif(($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F)) {
$utf8 .= $chrs{$c};
} elseif(($ord_chrs_c & 0xE0) == 0xC0) {
$utf8 .= substr($chrs, $c, 2); $c += 1;
} elseif(($ord_chrs_c & 0xF0) == 0xE0) {
$utf8 .= substr($chrs, $c, 3); $c += 2;
} elseif(($ord_chrs_c & 0xF8) == 0xF0) {
$utf8 .= substr($chrs, $c, 4); $c += 3;
} elseif(($ord_chrs_c & 0xFC) == 0xF8) {
$utf8 .= substr($chrs, $c, 5); $c += 4;
} elseif(($ord_chrs_c & 0xFE) == 0xFC) {
$utf8 .= substr($chrs, $c, 6); $c += 5;
}
}
return $utf8;
} elseif(preg_match('/^\[.*\]$/s', $str) || preg_match('/^{.*}$/s', $str)) {
if($str{0} == '[') {
$stk = array(HTML_AJAX_JSON_IN_ARR);
$arr = array();
} else {
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$stk = array(HTML_AJAX_JSON_IN_OBJ);
$obj = array();
} else {
$stk = array(HTML_AJAX_JSON_IN_OBJ);
$obj = new stdClass();
}
}
array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => 0, 'delim' => false));
$chrs = substr($str, 1, -1);
$chrs = $this->reduce_string($chrs);
if($chrs == '') {
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
return $arr;
} else {
return $obj;
}
}
$strlen_chrs = strlen($chrs);
for($c = 0; $c <= $strlen_chrs; $c++) {
$top = end($stk);
$substr_chrs_c_2 = substr($chrs, $c, 2);
if(($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == HTML_AJAX_JSON_SLICE))) {
$slice = substr($chrs, $top['where'], ($c - $top['where']));
array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) { // we are in an array, so just push an element onto the stack
array_push($arr, $this->decode($slice));
} elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) { // we are in an object, so figure out the property name and set an element in an associative array, for now
if(preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // "name":value pair
$key = $this->decode($parts[1]);
$val = $this->decode($parts[2]);
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
} elseif(preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // name:value pair, where name is unquoted
$key = $parts[1];
$val = $this->decode($parts[2]);
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
}
}
} elseif((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != HTML_AJAX_JSON_IN_STR)) { // found a quote, and we are not inside a string
array_push($stk, array('what' => HTML_AJAX_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
//print("Found start of string at {$c}\n");
} elseif(($chrs{$c} == $top['delim']) && ($top['what'] == HTML_AJAX_JSON_IN_STR) && (($chrs{$c - 1} != "\\") || ($chrs{$c - 1} == "\\" && $chrs{$c - 2} == "\\"))) { // found a quote, we're in a string, and it's not escaped
array_pop($stk);
//print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
} elseif(($chrs{$c} == '[') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-bracket, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_ARR, 'where' => $c, 'delim' => false));
//print("Found start of array at {$c}\n");
} elseif(($chrs{$c} == ']') && ($top['what'] == HTML_AJAX_JSON_IN_ARR)) { // found a right-bracket, and we're in an array
array_pop($stk);
//print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif(($chrs{$c} == '{') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-brace, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
//print("Found start of object at {$c}\n");
} elseif(($chrs{$c} == '}') && ($top['what'] == HTML_AJAX_JSON_IN_OBJ)) { // found a right-brace, and we're in an object
array_pop($stk);
//print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif(($substr_chrs_c_2 == '/*') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a comment start, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_CMT, 'where' => $c, 'delim' => false));
$c++;
//print("Found start of comment at {$c}\n");
} elseif(($substr_chrs_c_2 == '*/') && ($top['what'] == HTML_AJAX_JSON_IN_CMT)) { // found a comment end, and we're in one now
array_pop($stk);
$c++;
for($i = $top['where']; $i <= $c; $i++)
$chrs = substr_replace($chrs, ' ', $i, 1);
//print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
}
}
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
return $arr;
} elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) {
return $obj;
}
}
}
}
}
if ( !function_exists("json_encode") || !function_exists("json_decode") ) {
include_once "json.php";
function json_encode($data) {
$json = new HTML_AJAX_JSON();
return $json->encode($data);
}
function json_decode($data) {
$json = new HTML_AJAX_JSON();
return $json->decode($data);
}
}
?>
我给大家个采集接口,你爱自己采集可以用自己的FLASH播放器直接播放56.com的flv吧!不用帮人家做免费广告那么傻了!
代码如下:
<?php
$url = "http://www.56.com/u75/v_MTkwMTIwMjQ.html";
$html = @file_get_contents($url);
preg_match_all("/var _oFlv_o = '([\S|\s]+)'/isU", $html, $matches);
$o = decode56($matches[1][0]);
include_once "json.php"; //当php没有配置支持json的时候有用
$o = str_replace("'", '"', $o);
$o = json_decode($o);
//print_r($o);
$swf = "http://www.56.com/n_" . str_replace(":88", "", str_replace(".56.com", "", $o->img_host)) .
"_/" . str_replace(".56.com", "", $o->URL_host) .
"_/" . $o->URL_pURL .
"_/" . $o->URL_sURL .
"_/" . $o->user_id .
"_/" . $o->URL_URLid .
"_/" . $o->totaltime .
"_/" . $o->effectID .
"_/" . $o->id . ".swf";
$flv = "http://hot-chinacache.56.com/flvdownload/" . $o->URL_pURL .
"/" . $o->URL_sURL .
"/" . $o->user_id .
"@56.com_56flv_" . $o->URL_URLid . ".flv";
echo "FLASH文件路径: " . $swf;
echo "<br>";
echo "FLV文件路径: " . $flv;
$de = 0;
function decode56($str) {
global $de;
$c1 = "";
$c2 = "";
$c3 = "";
$c4 = "";
$c5 = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);
$c6 = charCodeAt($str, 0);
$c7 = "";
$i = "";
$len = "";
$out = "";
if(!$de) {
$de = 1;
$c7 = decode56($str);
$str = substr($c7, $c6);
}
$len = strlen($str);
$i = 1;
$out = "";
while ($i < $len) {
do {
$c1 = $c5[charCodeAt($str, $i++) & 0xff];
}
while($i < $len && $c1 == -1);
if ($c1 == -1) break;
do {
$c2 = $c5[charCodeAt($str, $i++) & 0xff];
}
while ($i < $len && $c2 == -1);
if($c2 == -1) break;
$out .= chr(($c1 << 2) | (($c2 & 0x30) >> 4));
do {
$c3 = charCodeAt($str, $i++) & 0xff;
if($c3 == 61) return $out;
$c3 = $c5[$c3];
}
while ($i < $len && $c3 == -1);
if($c3 == -1) break;
$out .= chr((($c2 & 0XF) << 4) | (($c3 & 0x3C) >> 2));
do {
$c4 = charCodeAt($str, $i++) & 0xff;
if($c4 == 61) return $out;
$c4 = $c5[$c4];
}
while ($i < $len && $c4 == -1);
if ($c4 == -1) break;
$out .= chr((($c3 & 0x03) << 6) | $c4);
}
return $out;
}
function charCodeAt($str, $i){
return ord(substr($str, $i, 1));
}
//该程序仅为技术交流,交流可加QQ群: 1714987
?>
json.php
代码如下:
<?php
define('HTML_AJAX_JSON_SLICE', 1);
define('HTML_AJAX_JSON_IN_STR', 2);
define('HTML_AJAX_JSON_IN_ARR', 4);
define('HTML_AJAX_JSON_IN_OBJ', 8);
define('HTML_AJAX_JSON_IN_CMT', 16);
define('HTML_AJAX_JSON_LOOSE_TYPE', 10);
define('HTML_AJAX_JSON_STRICT_TYPE', 11);
class HTML_AJAX_JSON
{
function HTML_AJAX_JSON($use=HTML_AJAX_JSON_STRICT_TYPE)
{
$this->use = $use;
}
function encode($var)
{
switch(gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false';
case 'NULL':
return 'null';
case 'integer':
return sprintf('%d', $var);
case 'double':
case 'float':
return sprintf('%f', $var);
case 'string': // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
$ascii = '';
$strlen_var = strlen($var);
for($c = 0; $c < $strlen_var; $c++) {
$ord_var_c = ord($var{$c});
if($ord_var_c == 0x08) {
$ascii .= '\b';
} elseif($ord_var_c == 0x09) {
$ascii .= '\t';
} elseif($ord_var_c == 0x0A) {
$ascii .= '\n';
} elseif($ord_var_c == 0x0C) {
$ascii .= '\f';
} elseif($ord_var_c == 0x0D) {
$ascii .= '\r';
} elseif(($ord_var_c == 0x22) || ($ord_var_c == 0x2F) || ($ord_var_c == 0x5C)) {
$ascii .= '\\'.$var{$c}; // double quote, slash, slosh
} elseif(($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)) {
// characters U-00000000 - U-0000007F (same as ASCII)
$ascii .= $var{$c}; // most normal ASCII chars
} elseif(($ord_var_c & 0xE0) == 0xC0) {
// characters U-00000080 - U-000007FF, mask 110XXXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1})); $c+=1;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xF0) == 0xE0) {
// characters U-00000800 - U-0000FFFF, mask 1110XXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2})); $c+=2;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xF8) == 0xF0) {
// characters U-00010000 - U-001FFFFF, mask 11110XXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3})); $c+=3;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xFC) == 0xF8) {
// characters U-00200000 - U-03FFFFFF, mask 111110XX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4})); $c+=4;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
} elseif(($ord_var_c & 0xFE) == 0xFC) {
// characters U-04000000 - U-7FFFFFFF, mask 1111110X, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4}), ord($var{$c+5})); $c+=5;
$ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'utf-8', 'UTF-8')));
}
}
return sprintf('"%s"', $ascii);
case 'array':
if(is_array($var) && (array_keys($var) !== range(0, sizeof($var) - 1)))
return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($var), array_values($var))));
return sprintf('[%s]', join(',', array_map(array($this, 'encode'), $var)));
case 'object':
$vars = get_object_vars($var);
return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($vars), array_values($vars))));
default:
return '';
}
}
function name_value($name, $value)
{
return (sprintf("%s:%s", $this->encode(strval($name)), $this->encode($value)));
}
function reduce_string($str)
{
$str = preg_replace('#^\s*//(.+)$#m', '', $str); // eliminate single line comments in '// ...' form
$str = preg_replace('#^\s*/\*(.+)\*/#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at start of string
$str = preg_replace('#/\*(.+)\*/\s*$#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at end of string
$str = trim($str); // eliminate extraneous space
return $str;
}
function decode($str)
{
$str = $this->reduce_string($str);
switch(strtolower($str)) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
if(is_numeric($str)) {
return ((float)$str == (integer)$str)
? (integer)$str
: (float)$str;
} elseif(preg_match('/^".+"$/s', $str) || preg_match('/^\'.+\'$/s', $str)) {
$delim = substr($str, 0, 1);
$chrs = substr($str, 1, -1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
for($c = 0; $c < $strlen_chrs; $c++) {
$substr_chrs_c_2 = substr($chrs, $c, 2);
$ord_chrs_c = ord($chrs{$c});
if($substr_chrs_c_2 == '\b') {
$utf8 .= chr(0x08); $c+=1;
} elseif($substr_chrs_c_2 == '\t') {
$utf8 .= chr(0x09); $c+=1;
} elseif($substr_chrs_c_2 == '\n') {
$utf8 .= chr(0x0A); $c+=1;
} elseif($substr_chrs_c_2 == '\f') {
$utf8 .= chr(0x0C); $c+=1;
} elseif($substr_chrs_c_2 == '\r') {
$utf8 .= chr(0x0D); $c+=1;
} elseif(($delim == '"') && (($substr_chrs_c_2 == '\\"') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
$utf8 .= $chrs{++$c};
} elseif(($delim == "'") && (($substr_chrs_c_2 == '\\\'') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
$utf8 .= $chrs{++$c};
} elseif(preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6))) { // single, escaped unicode character
$utf16 = chr(hexdec(substr($chrs, ($c+2), 2))) . chr(hexdec(substr($chrs, ($c+4), 2)));
$utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'utf-8');
$c+=5;
} elseif(($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F)) {
$utf8 .= $chrs{$c};
} elseif(($ord_chrs_c & 0xE0) == 0xC0) {
$utf8 .= substr($chrs, $c, 2); $c += 1;
} elseif(($ord_chrs_c & 0xF0) == 0xE0) {
$utf8 .= substr($chrs, $c, 3); $c += 2;
} elseif(($ord_chrs_c & 0xF8) == 0xF0) {
$utf8 .= substr($chrs, $c, 4); $c += 3;
} elseif(($ord_chrs_c & 0xFC) == 0xF8) {
$utf8 .= substr($chrs, $c, 5); $c += 4;
} elseif(($ord_chrs_c & 0xFE) == 0xFC) {
$utf8 .= substr($chrs, $c, 6); $c += 5;
}
}
return $utf8;
} elseif(preg_match('/^\[.*\]$/s', $str) || preg_match('/^{.*}$/s', $str)) {
if($str{0} == '[') {
$stk = array(HTML_AJAX_JSON_IN_ARR);
$arr = array();
} else {
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$stk = array(HTML_AJAX_JSON_IN_OBJ);
$obj = array();
} else {
$stk = array(HTML_AJAX_JSON_IN_OBJ);
$obj = new stdClass();
}
}
array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => 0, 'delim' => false));
$chrs = substr($str, 1, -1);
$chrs = $this->reduce_string($chrs);
if($chrs == '') {
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
return $arr;
} else {
return $obj;
}
}
$strlen_chrs = strlen($chrs);
for($c = 0; $c <= $strlen_chrs; $c++) {
$top = end($stk);
$substr_chrs_c_2 = substr($chrs, $c, 2);
if(($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == HTML_AJAX_JSON_SLICE))) {
$slice = substr($chrs, $top['where'], ($c - $top['where']));
array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) { // we are in an array, so just push an element onto the stack
array_push($arr, $this->decode($slice));
} elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) { // we are in an object, so figure out the property name and set an element in an associative array, for now
if(preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // "name":value pair
$key = $this->decode($parts[1]);
$val = $this->decode($parts[2]);
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
} elseif(preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // name:value pair, where name is unquoted
$key = $parts[1];
$val = $this->decode($parts[2]);
if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
}
}
} elseif((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != HTML_AJAX_JSON_IN_STR)) { // found a quote, and we are not inside a string
array_push($stk, array('what' => HTML_AJAX_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
//print("Found start of string at {$c}\n");
} elseif(($chrs{$c} == $top['delim']) && ($top['what'] == HTML_AJAX_JSON_IN_STR) && (($chrs{$c - 1} != "\\") || ($chrs{$c - 1} == "\\" && $chrs{$c - 2} == "\\"))) { // found a quote, we're in a string, and it's not escaped
array_pop($stk);
//print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
} elseif(($chrs{$c} == '[') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-bracket, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_ARR, 'where' => $c, 'delim' => false));
//print("Found start of array at {$c}\n");
} elseif(($chrs{$c} == ']') && ($top['what'] == HTML_AJAX_JSON_IN_ARR)) { // found a right-bracket, and we're in an array
array_pop($stk);
//print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif(($chrs{$c} == '{') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-brace, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
//print("Found start of object at {$c}\n");
} elseif(($chrs{$c} == '}') && ($top['what'] == HTML_AJAX_JSON_IN_OBJ)) { // found a right-brace, and we're in an object
array_pop($stk);
//print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif(($substr_chrs_c_2 == '/*') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a comment start, and we are in an array, object, or slice
array_push($stk, array('what' => HTML_AJAX_JSON_IN_CMT, 'where' => $c, 'delim' => false));
$c++;
//print("Found start of comment at {$c}\n");
} elseif(($substr_chrs_c_2 == '*/') && ($top['what'] == HTML_AJAX_JSON_IN_CMT)) { // found a comment end, and we're in one now
array_pop($stk);
$c++;
for($i = $top['where']; $i <= $c; $i++)
$chrs = substr_replace($chrs, ' ', $i, 1);
//print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
}
}
if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
return $arr;
} elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) {
return $obj;
}
}
}
}
}
if ( !function_exists("json_encode") || !function_exists("json_decode") ) {
include_once "json.php";
function json_encode($data) {
$json = new HTML_AJAX_JSON();
return $json->encode($data);
}
function json_decode($data) {
$json = new HTML_AJAX_JSON();
return $json->decode($data);
}
}
?>
[2]从MySQL数据库表中取出随机数据的代码
来源: 互联网 发布时间: 2013-11-30
MySQL 如何从表中取出随机数据
以前在群里讨论过这个问题,比较的有意思.mysql的语法真好玩.
他们原来都想用PHP的实现随机,但取出多条好像要进行两次以上查询.
翻了手册,找到了下面这个语句,可以完成任务了
SELECT * FROM table_name ORDER BY rand() LIMIT 5;
rand在手册里是这么说的:
RAND()
RAND(N)
返回在范围0到1.0内的随机浮点值。如果一个整数参数N被指定,它被用作种子值。
mysql> select RAND();
-> 0.5925
mysql> select RAND(20);
-> 0.1811
mysql> select RAND(20);
-> 0.1811
mysql> select RAND();
-> 0.2079
mysql> select RAND();
-> 0.7888
你不能在一个ORDER BY子句用RAND()值使用列,因为ORDER BY将重复计算列多次。然而在MySQL3.23中,你可以做: SELECT * FROM table_name ORDER BY RAND(),这是有利于得到一个来自SELECT * FROM table1,table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000的集合的随机样本。注意在一个WHERE子句里的一个RAND()将在每次WHERE被执行时重新评估。
但我试了一下,8千条记录的表,执行一次需要0.08 sec,.慢了些
后来请教了google,得到如下代码
SELECT *
FROM table_name AS r1 JOIN
(SELECT ROUND(RAND() *
(SELECT MAX(id)
FROM table_name)) AS id)
AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 5;
执行效率需要0.02 sec.可惜的是,只有mysql 4.1.*以上才支持这样的子查询.
以前在群里讨论过这个问题,比较的有意思.mysql的语法真好玩.
他们原来都想用PHP的实现随机,但取出多条好像要进行两次以上查询.
翻了手册,找到了下面这个语句,可以完成任务了
SELECT * FROM table_name ORDER BY rand() LIMIT 5;
rand在手册里是这么说的:
RAND()
RAND(N)
返回在范围0到1.0内的随机浮点值。如果一个整数参数N被指定,它被用作种子值。
mysql> select RAND();
-> 0.5925
mysql> select RAND(20);
-> 0.1811
mysql> select RAND(20);
-> 0.1811
mysql> select RAND();
-> 0.2079
mysql> select RAND();
-> 0.7888
你不能在一个ORDER BY子句用RAND()值使用列,因为ORDER BY将重复计算列多次。然而在MySQL3.23中,你可以做: SELECT * FROM table_name ORDER BY RAND(),这是有利于得到一个来自SELECT * FROM table1,table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000的集合的随机样本。注意在一个WHERE子句里的一个RAND()将在每次WHERE被执行时重新评估。
但我试了一下,8千条记录的表,执行一次需要0.08 sec,.慢了些
后来请教了google,得到如下代码
SELECT *
FROM table_name AS r1 JOIN
(SELECT ROUND(RAND() *
(SELECT MAX(id)
FROM table_name)) AS id)
AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 5;
执行效率需要0.02 sec.可惜的是,只有mysql 4.1.*以上才支持这样的子查询.
[3]小结下MySQL中文乱码,phpmyadmin乱码,php乱码 产生原因及其解决方法第1/3页
来源: 互联网 发布时间: 2013-11-30
乱码产生原因
mysql字符编码是版本4.1引入的,支持多国语言,而且一些特性已经超过了其他的数据库系统。
我们可以在MySQL Command Line Client 下输入如下命令查看mysql的字符集
mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
36 rows in set (0.02 sec)
更多mysql的字符集知识可以参考本论坛的
http://www.phpfans.net/bbs/viewt ... &extra=page%3D1
或者mysql官方的
http://dev.mysql.com/doc/refman/5.1/zh/charset.html
MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。
查看系统的字符集和排序方式的设定可以通过下面的两条命令:
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+-------------------------------------------+
8 rows in set (0.06 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.02 sec)
上面列出的值就是系统的默认值。latin1默认校对规则是latin1_swedish_ci,默认是latin1的瑞典语排序方式.
为什么呢默认会是latin1_swedish_ci呢,追溯一下mysql历史很容易发现
1979年,一家瑞典公司Tcx欲开发一个快速的多线程、多用户数据库系统。Tcx 公司起初想利用mSQL和他们自己的快速低级例程 (Indexed Sequential Access Method,ISAM)去连接数据库表,然而,在一些测试以后得出结论:mSQL对其需求来说不够快速和灵活。这就产生了一个连接器数据库的新SQL接口,它使用几乎和mSQL一样的API接口。这个API被设计成可以使那些由mSQL而写的第三方代码更容易地移植到MySQL。
相信如果mysql是中国开发的,那么汉语也是默认编码了
当然我们也可以自己需要修改mysql的默认字符集
在mysql配置文档my.ini,找到如下两句:
[mysql]
default-character-set=latin1
和
# created and no character set is defined
default-character-set=latin1
修改后面的值就可以。
这里不建议改,仍保留默认值
也就是说启动 mysql时,如果没指定指定一个默认的的字符集,这个值继承自配置文件中的;
此时 character_set_server 被设定为这个默认的字符集; 当创建一个新的数据库时,
除非明确指定,这个数据库的字符集被缺省设定为 character_set_server; 当选定了一个数据库时,
character_set_database 被设定为这个数据库默认的字符集; 在这个数据库里创建一张表时,
表默认的字符集被设定为 character_set_database,也就是这个数据库默认的字符集;
当在表内设置一栏时,除非明确指定,否则此栏缺省的字符集就是表默认的字符集。
这样问题就随之而来了,假如一数据库是gbk编码。如果访问数据库时没指定其的字符集是gbk。
那么这个值将继承系统的latin1,这样就做成mysql中文乱码。
乱码解决方法
要解决乱码问题,首先必须弄清楚自己数据库用什么编码。如果没有指明,将是默认的latin1。
我们用得最多的应该是这3种字符集 gb2312,gbk,utf8。
那么我们如何去指定数据库的字符集呢?下面也gbk为例
【在MySQL Command Line Client创建数据库 】
mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql字符编码是版本4.1引入的,支持多国语言,而且一些特性已经超过了其他的数据库系统。
我们可以在MySQL Command Line Client 下输入如下命令查看mysql的字符集
mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
36 rows in set (0.02 sec)
更多mysql的字符集知识可以参考本论坛的
http://www.phpfans.net/bbs/viewt ... &extra=page%3D1
或者mysql官方的
http://dev.mysql.com/doc/refman/5.1/zh/charset.html
MySQL 4.1的字符集支持(Character Set Support)有两个方面:字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次: 服务器(server),数据库(database),数据表(table)和连接(connection)。
查看系统的字符集和排序方式的设定可以通过下面的两条命令:
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+-------------------------------------------+
8 rows in set (0.06 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.02 sec)
上面列出的值就是系统的默认值。latin1默认校对规则是latin1_swedish_ci,默认是latin1的瑞典语排序方式.
为什么呢默认会是latin1_swedish_ci呢,追溯一下mysql历史很容易发现
1979年,一家瑞典公司Tcx欲开发一个快速的多线程、多用户数据库系统。Tcx 公司起初想利用mSQL和他们自己的快速低级例程 (Indexed Sequential Access Method,ISAM)去连接数据库表,然而,在一些测试以后得出结论:mSQL对其需求来说不够快速和灵活。这就产生了一个连接器数据库的新SQL接口,它使用几乎和mSQL一样的API接口。这个API被设计成可以使那些由mSQL而写的第三方代码更容易地移植到MySQL。
相信如果mysql是中国开发的,那么汉语也是默认编码了
当然我们也可以自己需要修改mysql的默认字符集
在mysql配置文档my.ini,找到如下两句:
[mysql]
default-character-set=latin1
和
# created and no character set is defined
default-character-set=latin1
修改后面的值就可以。
这里不建议改,仍保留默认值
也就是说启动 mysql时,如果没指定指定一个默认的的字符集,这个值继承自配置文件中的;
此时 character_set_server 被设定为这个默认的字符集; 当创建一个新的数据库时,
除非明确指定,这个数据库的字符集被缺省设定为 character_set_server; 当选定了一个数据库时,
character_set_database 被设定为这个数据库默认的字符集; 在这个数据库里创建一张表时,
表默认的字符集被设定为 character_set_database,也就是这个数据库默认的字符集;
当在表内设置一栏时,除非明确指定,否则此栏缺省的字符集就是表默认的字符集。
这样问题就随之而来了,假如一数据库是gbk编码。如果访问数据库时没指定其的字符集是gbk。
那么这个值将继承系统的latin1,这样就做成mysql中文乱码。
乱码解决方法
要解决乱码问题,首先必须弄清楚自己数据库用什么编码。如果没有指明,将是默认的latin1。
我们用得最多的应该是这3种字符集 gb2312,gbk,utf8。
那么我们如何去指定数据库的字符集呢?下面也gbk为例
【在MySQL Command Line Client创建数据库 】
mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)
最新技术文章: