how to store all facebook emoji in mysql database - php

I am using Emoji character in my project for facebook post. Some emoji character are saved good but some characters are saved like (??) into mysql database. My database table Default collation is utf8mb4_unicode_ci .
Is there any way to save all emoji into database.
I am working in Cakephp3.
My database structure is :
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => '******',
'password' => '******',
'database' => '******',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'url' => env('DATABASE_URL', null),
],

I had a similar problem to this when dealing with unusual characters such as this. The issue wasnt with the database, it was with how i was connecting to the database. Since you have provided no code, I can't give any specific advice, but check your DB connection string and make sure its using the mb4 version of utf8. Otherwise the response will not contain the characters.
If you are using PDO, then your db string will look like this.
$db = 'mysql:host=example.com;dbname=testdb;port=1234;charset=utf8mb4';

Related

Connecting to MySQL database with SSH key in CakePHP3

I'm a super rookie developer so be nice!
I have web hosting and database through GoDaddy. It's linked to MySQL Workbench but need it to be linked to my application, where I'm using CakePHP3.
Can someone please help me with how to enter the right host names, usernames etc. when there's an SSH key involved? See the image for the database credentials to help me fill in the blanks below:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '???',
'port' => '3306',
'username' => '???',
'password' => '???',
'database' => 'EquineEventFinder',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
DB details image - see for db details.

Laravel 4.2 Eloquent only returning 1MB data

I have a mediumtext field in my mySQL database and it only return 1MiB data (a string with length 1048576) with the later part of the string trimmed when I query the database. A common solution I found online is adding this option to database.php config file.
'options' => array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216
)
However, it still only returns 1MiB of data. What else can be the reason causing the problem?
It turns out to be the problem when making connection to another database. I thought the default settings will be loaded automatically if not set, but it seems it is necessary to set the options even if I am using the same connection profile.
//.......... Some other php code ....................
Config::set('database.connections.mysql', array(
'driver' => 'mysql',
'host' => $db_host,
'database' => $db_name,
'username' => $db_user,
'password' => $db_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'options' => array( PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216 ), //This line was missed
));
//.......... Some other php code ....................

Dynamic database connection in Laravel

I know that in Laravel you can use multiple database connections by specifying them in the config/database.php file, then using DB::connection('my_conn_name'), but is there anyway to use a connection that is not specified in the config/database.php file?
I am writing an archiving application, so the user can specify what connection they would like to use for the process (host, user and password), and I am hoping that I can return the results from show databases for the supplied connection.
After the user has specified the db parameters you could store it in a session to populate a custom connection at config/database.php:
'connections' => [
'mysql' => [
'...'
],
'testing' => [
'...'
],
'custom' => [
'driver' => 'mysql',
'host' => session()->get()->db_host,
'database' => session()->get()->db_database,
'username' => session()->get()->db_username,
'password' => session()->get()->db_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]

Is It Possible to use Laravel 4 without a Default Database

I'm creating an application that reads information from a number of different databases, but doesn't actually have its own database, as there is no information being written anywhere.
Basically, a user selects a record and a type, and the application will generate a .pdf file based on their choices. I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. Is there a way to tell Laravel not to connect to a database? I've tried a few things (all in app/config/database.php), first being:
'default' => NULL,
// and
//'default' => '',
Which both return:
Undefined index: driver
I've also tried:
'default' => 'none',
'connections' => array(
'none' => array(
'driver' => '',
'host' => '',
...
),
),
which in turn returns:
Unsupported driver []
Unsupported host[]
...
And lastly setting 'default' => '', which returns:
Database [] not configured.
I've found ways to use Laravel's models without a database connection, but not actually Laravel itself.
Edit:
Setting the connection to an existing mysql connection and not selecting a database "works":
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
but I'm not sure if that's the right way to do it. It feels like a work-around and not an actual solution.
I would do the following:
'driver' => 'sqlite',
and for the sqlite connection settings
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]
That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.
You could also remove the database service provider from your app.php config file:
'Illuminate\Cookie\CookieServiceProvider',
//'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
and the facades for Fluent and Eloquent in the same file:
'Crypt' => 'Illuminate\Support\Facades\Crypt',
//'DB' => 'Illuminate\Support\Facades\DB',
//'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.
What you need to do define the connection in the app/config/database.php file and specify the connection when running a query with laravel's DB::connection('connection_name').
More information here --> http://laravel.com/docs/4.2/database#accessing-connections
Simply define your db driver as 'sqlite' in the database config.
'default' => 'sqlite',
As long as you haven't changed anything in the default sqlite config section, all should work. You don't necessarily have to use the database, but it will alleviate those errors you received.
In case you have changed the default sqlite config for whatever reason, you can follow the below steps to configure the sqlite db.
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
Also, make sure this 'production.sqlite' file exists if it doesn't already. You can do this easily from the command line:
touch app/database/production.sqlite

Cakephp Sqlserver encoding

This has me stumped. I am trying to set encoding for my Sqlserver connection and all that I have tried has failed. I only get
Error: A Database connection using "Sqlserver" was missing or unable
to connect. The database server returned this error:
SQLSTATE[IMSSP]: An invalid encoding was specified for
SQLSRV_ATTR_ENCODING.
The original error I was trying to solve through encoding is:
Error: SQLSTATE[IMSSP]: An error occurred translating the query string
to UTF-16: No mapping for the Unicode character exists in the target
multi-byte code page.
The SQL version is 2008 R2
Cakephp Version: 2.4.2
PHP Version: 5.3.27
After a lot of trial and error this works:
public $default = array(
'datasource' => 'Database/Sqlserver',
'persistent' => false,
'host' => 'localhost',
'login' => 'sa',
'password' => 'password',
'database' => 'SchedulingDatabase',
'encoding' => PDO::SQLSRV_ENCODING_UTF8
);
Tried this out with Cakephp 3.0 seems to work nicely.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlserver',
'persistent' => false,
'host' => 'localhost',
'port' => '1433', //using this port
'username' => 'sa',
'password' => 'password',
'database' => 'cake_bookmarks',
'encoding' => PDO::SQLSRV_ENCODING_UTF8,
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
]
SQL 2008 doesn't support UTF-16 only SQL 2012
http://msdn.microsoft.com/en-us/library/ms143726(v=sql.110).aspx
UTF-8 isn't supported at all by MS SQL.

Categories