Doctrine 2 + cyrillic - php

I am trying to read an utf8 (cyrillic) data from database with Doctrine2 ORM, but it always returns unreadable symbols.
My database, table and text field in utf8-general-ci.
The connection looks like this:
$em = EntityManager::create(array(
'driver' => 'pdo_mysql',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME
), $config);
But still without success. How can I solve it?
Thank you

Here is the solution I've found and it works:
$em = EntityManager::create(array(
'driver' => 'pdo_mysql',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME
), $config);
$em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci'));

Related

Disconnecting a Laravel Connection

I am having some trouble with disconnecting my created conn.
$dbName = session("key")["db_name"]["0"]->main_db;
$oldEmail = $this->connection()->table("tbl_user")->where("id",$UserID)->get();
DB::disconnect($dbName);
$User = User::where("email",$email)->get();
return $User;
I think the connection bypass my model and search for the table name of "User" in my created connection, I don't get the error, though it has the same name in my migration.
This is my codes in generating another connection:
public function connection() {
// SESSION
$dbName = session("key")["db_name"]["0"]->main_db;
// MAKE CONNECTION
Config::set("database.connections.".$dbName, array(
"driver" => config("app.DB_CONNECTION"),
"host" => config("app.DB_HOST"),
"database" => $dbName,
"username" => config("app.DB_USERNAME"),
"password" => config("app.DB_PASSWORD"),
));
DB::setDefaultConnection($dbName);
$conn = DB::connection($dbName);
return $conn;
}
app/config/database.php
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Useful Links
https://laracasts.com/discuss/channels/eloquent/laravel-5-multiple-database-connection

Connect to Different Database in October CMS

Connecting to Different Database in October CMS
Hello,
I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.
I want to connect to the other database and fetch information from one table and display on my home page.
Please help me with this. I am newbie to this CMS.
Thank you.
In your config/database.php you will have something like this (assuming MySQL):
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Just add another one after it, changing the name to something else (mysql-legacy in my example below) and the relevant connection details:
'mysql-legacy' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
After that, you can use DB; in your model or controller and then do things like this:
$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');

Returning a PDOException when trying to connect to MySQL Database

I am returning a PDOException saying I could not connect to the server and I cannot figure out why... I am Following PHPAccademy's guide on YouTube.
This is the code used to define the connection parameters
'db' => [
'driver' => 'mysql',
'host' => '107.170.30.229',
'name' => 'bsa',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8mb4_general_ci',
'prefix' => ''
],
This is the code that is used to connect to the database
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => $app->config->get('db.driver'),
'host' => $app->config->get('db.host'),
'database' => $app->config->get('db.name'),
'username' => $app->config->get('db.username'),
'password' => $app->config->get('db.password'),
'charset' => $app->config->get('db.charset'),
'collation' => $app->config->get('db.collation'),
'prefix' => $app->config->get('db.prefix'),
]);
$capsule->bootEloquent();
Firstly, please DO NOT post any username/passwords onto SO, regardless of development or not, not everybody reading has a good heart.
Secondly, as i was able to log into your server and test this myself, it seems that you need to use localhost instead of defining your server IP for the host.
So please use:
'host' => 'localhost',
Instead of:
'host' => '107.170.30.229',

Connection timeout with Zend_Db::factory using PDO_MYSQL

What is the proper (or better) way to set connection timeout with PHP code, when using Zend_Db::factory with PDO_MYSQL.
I have:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
a.)
$db->getConnection()->setAttribute(PDO::ATTR_TIMEOUT, 600);
b.)
$sql = "SET SESSION wait_timeout = 600";
$db->getConnection()->query($sql);
Reduce the connection timeout to 3 seconds:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
'driver_options' => [
\PDO::ATTR_TIMEOUT => 3
]
);
$db = Zend_Db::factory('PDO_MYSQL', $params);

laravel database connection returns undefined index error

I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.

Categories