开发者

Is there a way to get a MySQL table defined encoding in PHP?

开发者 https://www.devze.com 2023-01-01 00:42 出处:网络
I see I can get a MySQL database defined encoding in PHP using the function mysql_client_encoding(...), but is th开发者_JAVA百科ere a way to get a MySQL table defined encoding in PHP?I see no easy way

I see I can get a MySQL database defined encoding in PHP using the function mysql_client_encoding(...), but is th开发者_JAVA百科ere a way to get a MySQL table defined encoding in PHP?


I see no easy way to retrieve this info. The best I could do is to do a "SHOW CREATE TABLE ;" and parse the answer:

<?php

$link = mysql_connect('localhost', 'account', 'passwd');
mysql_select_db('my_base');

$q = mysql_query('show create table my_table;');
$row = mysql_fetch_assoc($q);

preg_match("/CHARSET=(.*)/", $row['Create Table'], $matched);
echo "Table was created with charset " . $matched[1];

Which gives me:

Table was created with charset utf8

Note that charset may not be present if your table was not created with this info.


I am using SHOW CREATE TABLE query, but never from PHP.
Because I can't imagine if I ever need to run it from PHP. SET NAMES can serve any possible case.

Note that mysql_client_encoding() returns not database encoding. It's client encoding, as it says


As far as I know the encoding is defined on a per database basis in MySQL and not per table.

0

精彩评论

暂无评论...
验证码 换一张
取 消