pdo dblib stored procedure insert wrong characters - sql-server-2008

i'm using microsoft sql server as database and my php code on centos server. using freetds and dblib to connect from yii framework to mssql.
everything's are fine. after insert into db by Stored Procedure, data saved but in database query we have ??????? in this NVARCHAR column.
my data in utf-8 arabic. this is my configuration and code:
fretds.conf
[mss]
host = 172.31.1.2
ip = 172.31.1.2
port = 1433
tds version = 7.0
yii main configuration
'db'=>array(
'connectionString' => 'dblib:host=mss;database=XXXX;charset=utf8',
'username' => 'XXX',
'password' => 'XXXXXXXX',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
'enableProfiling' => true,
'schemaCachingDuration' => 5 * 60 * 60,
),
my model Stored Procedure
$builder=$this->getCommandBuilder();
$table=$this->getMetaData()->tableSchema;
$command=$builder->createSqlCommand('EXEC dbo.sp_link_comment_insert :link_id, :cmnt_parent_id, :user_id, :cmnt_status, :cmnt_text, :cmnt_thread',
array(
':link_id'=>58829,
':cmnt_parent_id'=>'',
':user_id'=>9,
':cmnt_status'=>1,
':cmnt_text'=>'تست ارسال comment',
':cmnt_thread'=>0,
)
);
$command->execute();

A late reply. Try specifying the encoding in the freetds configuration using the option client charset = UTF-8. Your config file would look like this after the change:
[mss]
host = 172.31.1.2
ip = 172.31.1.2
port = 1433
tds version = 7.0
client charset = UTF-8
According to the docs chars not recognized are converted to ?.
If FreeTDS runs into a character it can not convert, its behavior
varies according to the severity of the problem. On retrieving data
from the server, FreeTDS substitutes an ASCII '?' in the character's
place, and emits a warning message stating that some characters could
not be converted.
Reference: http://freetds.schemamania.org/userguide/localization.htm

Your driver (freeTDS) does not support UTF-8 for MSSQL. The only driver that natively supports UTF-8 for MSSQL is SQLDRV driver available only for windows platform.
To store data in UTF-8 using any other than SQLDRV driver (SQLDRV is not available for linux at the moment), the application must take care of converting from source charset into UTF-8 and vice versa. Therefore, setting "charset=UTF8" in your connection string has no effect.
In PHP, you should convert data before storing into MSSQL like so:
$dataUTF8 = iconv('windows-1250', 'utf-8', $data);
$sql = "INSERT INTO table (value) VALUES ($dataUTF8)";
and convert them back when reading from MSSQL:
$sql = "SELECT dataUTF8 FROM table;";
$data = iconv('utf-8', 'windows-1250', $dataUTF8);

Related

julia mysql.jl use non default port

I'm trying to connect to a MySQL database using the mySQl.jl package. it seems to work fine when I'm using the standard mySQL port 3306, but I don't see where to specify a database on a different port. How is that accomplished?
From the help doc of mysql_connect (get it with ?mysql_connect at the REPL):
mysql_connect(host::String, user::String, passwd::String,
db::String = ""; port::Int64 = MYSQL_DEFAULT_PORT,
socket::String = MYSQL_DEFAULT_SOCKET, opts = Dict())
Connect to a MySQL database.
So just add a named parameter port= after the database name parameter. For example:
mysql_connect("localhost", "john", "password", "my_db", port=1234)

CakePHP returns ?? when querying MySQL for Chinese Characters

We started adding Chinese language support to our application. Our DB Charset is set to UTF8 and I can successfully query from MySQL CLI and see the word in Chinese characters
However, when I try to use CakePHP query method "findAll", the value is returned as ?? instead of the Chinese characters
Our DB config in database.php looks like this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '******',
'database' => '******',
'prefix' => '******',
'encoding' => 'utf8'
);
Also we have this line set in core.php:
Configure::write('App.encoding', 'UTF-8');
We don't have any views set for this, we are using REST APIs so we are mainly just doing an "echo" in the Controller method for whatever response we get from the DB.
I also tried to create a test file and write this code in it:
<?php echo '基本' ?>
And it worked fine.
Any suggestions on how to get this to work?
Turned out that I needed to install php-mbstring which wasn't installed
Once I installed it and restarted apache, it worked like a charm

Cannot connect to Google Cloud MySQL using SSL with PDO on Google Compute

Just figured this out and wanted to share.
I am able to connect to MySQL using SSL with the PHP PDO from my local machine. The same code fails when run from a Google Compute Engine instance. I know the certs and IP address are setup correctly because connecting via the MySQL command line client using SSL on the instance works perfectly.
MySQL command line works on Compute:
mysql --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem \
--host=111.111.111.111 --user=someuser --password
PHP PDO does not work on Compute:
<?php
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword,
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem'
)
);
PDO gives this error on Compute:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2026] SSL connection error: ASN: bad other signature confirmation'
Remove the CA cert from the PDO connection.
Remove this key/value from the array:
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem'
Like so:
<?php
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword,
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem'
)
);
PDO with SSL will silently fail back to an insecure connection under certain conditions. To test that SSL is working run this query on the connection:
SHOW STATUS LIKE 'Ssl_cipher';
It should return something like:
Ssl_cipher => DHE-RSA-AES256-SHA
Otherwise the it will return an empty value.
Just add one more PDO attribute (as per comments by Desislav Kamenov in http://php.net/manual/en/ref.pdo-mysql.php) into your array so that your PDO becomes ...
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword',
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
)
);

Kohana - Database_Exception [ 2 ]: mysql_connect() 2

I am trying to setup my database config as demonstrated here: http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/
I have cut and pasted the database.php with my mysql credentials. I then get an error which I fixed by renaming the file to Database.php. the next issue is that my credentials don't change no matter what I put in.
The error shows:
Database_Exception [ 2 ]: mysql_connect() [function.mysql-connect]: Access denied for user 'ivanh'#'localhost' (using password: NO)
MODPATH/database/classes/Kohana/Database/MySQL.php [ 67 ]
62 catch (Exception $e)
63 {
64 // No connection exists
65 $this->_connection = NULL;
66
67 throw new Database_Exception(':error',
68 array(':error' => $e->getMessage()),
69 $e->getCode());
70 }
71
72 // \xFF is a better delimiter, but the PHP driver uses underscore
So no matter what I insert, username, host and password values do not change.
My code in Database.php is then copied from: http://kohanaframework.org/3.3/guide/database/config
firstly with my own database values then after as is. With no luck.
Other info: I'm creating this on a shared unix host.
Kohana 3.1.4: Database_Exception [ 2 ]: mysql_connect(): Access denied seems like a similar issue without a resolution.
Any help would be great, thanks.
Try These Steps:
Step 1:
Go to bootstrap.php
path- kohana\application\bootstrap.php
Un-comment the below line for database access
'database' => MODPATH.'database', // Database access
Step 2:
Go to Your config folder,
create the database.php file if not created, with below codes by giving your MySql credential
Path: kohana\application\config\database.php
Notes: First create a database in your Mysql
If the problem is not solved check your MySql settings too..there may be a error
<?php defined('SYSPATH') OR die('No direct access allowed.');
return array
(
'default' =>
array
(
'type' => 'MySQL',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
* array variables system variables as "key => value" pairs
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'localhost',
'database' => 'CHANGE YOUR DATABASE NAME HERE',
'username' => 'CHANGE YOUR USER NAME HERE',
'password' => 'CHANGE YOUR PASSWORD HERE',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
),
'remote' => array(
'type' => 'MySQL',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn Data Source Name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*/
'dsn' => 'mysql:host=localhost;dbname=CHANGE YOUR DATABASE NAME HERE',
'username' => 'CHANGE YOUR USER NAME HERE',
'password' => 'CHANGE YOUR PASSWORD HERE',
'persistent' => FALSE,
),
/**
* The following extra options are available for PDO:
*
* string identifier set the escaping identifier
*/
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
);
I am going to make the following assumptions:
You are consequently writing "Database.php" because the file in APPPATH/config containing the code you copied does indeed start with a capital letter.
You haven't changed the config reader.
Database::instance() will request a config array from the Kohana::$config if none is supplied. Config_File::load() will then loop through all found /config/database.php's found in APPPATH, enabled modules and SYSPATH.
Unix filesystems are case sensitive, notice the lowercase 'd' Kohana is looking for versus your uppercase 'D'.
If you did name it 'database.php' and not 'Database.php', click on 'Environment' and then 'Included files' on the error page and make sure it is included. If it's not, do some backtracking and try to find out when things stop behaving as they should, followed by why.
Edit: I don't know how I missed the "renamed it to Database.php" part, but that did not fix anything.

Charset string appended with dbname when using jdbc to connect to Mysql in Matlab

I am trying to set the charset when letting Matlab connect to remote mysql database server.
The connection url is like this:
jdbc:mysql://host/mtdb?useUnicode=true&characterEncoding=UTF8
And after executing:
c = database("mydb", 'username', 'password','com.mysql.jdbc.Driver', "connection_string as above");
But Matlab throws an exception:
'Unsupported character encoding 'UTF8mydb'.
I cannot see why character encoding is appended with "mydb". I don't see any syntax error in the connection url format.
Try this instead:
dbURL = 'jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=UTF8';
conn = database('', 'user', 'pass', 'com.mysql.jdbc.Driver', dbURL)
curs = exec(conn, 'select * from table')