Zend framework 3 Mysqli adapter issue - zend-framework

I am trying to connect mysql to zend framework 3 application but it gives such result as below:
Class 'Zend\Db\Adapter\Adapter\Driver\Mysqli' not found
I am using below code:
$config = [
'driver' => 'Mysqli',
'database' => 'abc',
'host' => 'localhost',
// 'port' => 27017,
'username' => 'root',
'password' => '',
];
$adapter = new Mysqli($config);
$result = $adapter->query("select * from
table1",Adapter::QUERY_MODE_EXECUTE);
foreach($result as $row){
echo $row->sentence;"<br>";
}
inserted this as well. use Zend\Db\Adapter\Adapter\Driver\Mysqli;

Related

DB::connection() Laravel 5 not working

I'am using Laravel 5.0 and I make 2 database which is one for system and two for data. when I try to change connection to get data from my second database it tell error
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "barang" does not
exist LINE 1: select * from barang
Here my Controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class MultiController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$data = DB::connection('pgsql2')->select('select * from barang');
return view('laporan.db')->withData($data);
}
Database.php
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'pgsql',
'connections' => [
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larasimak'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'pgsql2' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'simkie_data'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
my DotEnv
APP_ENV=local
APP_DEBUG=true
APP_KEY=nlXoLNFcWAD9rtTGXCUSpDdbQxms1ADi
DB_HOST=localhost
DB_DATABASE=larasimak
DB_USERNAME=postgres
DB_PASSWORD=postgres
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
You should set the credential of second database. Current both database are using same credential. Try following for second database:
database.php
'pgsql2' => [
'driver' => 'pgsql',
'host' => env('PS2_DB_HOST', 'localhost'),
'database' => env('PS2_DB_DATABASE', 'simkie_data'),
'username' => env('PS2_DB_USERNAME', 'postgres'),
'password' => env('PS2_DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
]
.evn
PS2_DB_HOST=localhost
PS2_DB_DATABASE=simkie_data
PS2_DB_USERNAME=postgres
PS2_DB_PASSWORD=postgres

Lumen connecting to multiple databases using DB_ connections in .env - Possible?

I can see how to have multiple connections using a configuration file in app/config/database.php and this is well documented.
Is this the only way or can you also define multiple connections using the .env file?
First, you'll need to configure your connections. You need to create a config directory in your project and add the file config/database.php. Like this:
<?php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead2',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name'). See below for a full example.
<?php
namespace App\Http\Controllers;
use Illuminate\Database\DatabaseManager;
class StatsController extends Controller
{
/**
* #return array
*/
public function getLatest()
{
// Resolve dependencies out of container
/** #var DatabaseManager $db */
$db = app('db');
$database1 = $db->connection('mysql');
$database2 = $db->connection('mysql2');
// Look up 3 newest users and 3 newest blog posts
$threeNewestUsers = $database1->select("SELECT * FROM users ORDER BY created_at DESC LIMIT 3");
$threeLatestPosts = $database2->select("SELECT * FROM blog_posts ORDER BY created_at DESC LIMIT 3");
return [
"new_users" => $threeNewestUsers,
"new_posts" => $threeLatestPosts,
];
}
}
http://andyfleming.com/configuring-multiple-database-connections-in-lumen-without-facades/

Yii2:- update record using update query for mongodb

I am trying to make update query using Yii2 for mongodb. I make following query:
$collection = Yii::$app->mongodb->getCollection('usermaster');
$arrUpdate = [
'firstName' => $fname,
'lastName' => $lname,
'email' => $email,
'is_visible' => $isvisibleUser,
'phoneNumber' => $phone,
'userName' => $uname,
];
$collection->update(['_id = 55a4957sd88423d10ea7c07d'],$arrUpdate);
But it shows follwoing error in firebug.
"NetworkError: 500 Internal Server Error - http://localhost/yii2angularseedmaster/frontend/web/category/corporateupdate?corpUserid=55a4957sd88423d10ea7c07d"
My insert query works perfectly but update query does not works.
Please tell me what is wrong in my query.
$collection->update(['_id' => $id],$arrUpdate);
Your code should be like this
$collection = Yii::$app->mongodb->getCollection('usermaster');
$arrUpdate = [
'firstName' => $fname,
'lastName' => $lname,
'email' => $email,
'is_visible' => $isvisibleUser,
'phoneNumber' => $phone,
'userName' => $uname,
];
$collection->update(['_id' => $id],$arrUpdate);

Connect With PostgreSQL in Phalcon Framework

Phalcon is not able to connect to postgrsql. Here are my settings in config.php
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'postgres',
'password' => 'root',
'dbname' => 'mydb',
'charset' => 'utf8',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'baseUri' => '/test/',
)
));
Page is blank showing no errors.
DI service implementation
use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
"charset" => $config->database->charset
));
});
There is no port in db connection array you used. Default postgresql port is 5432. so use this in your db connection and try to pass array directly to postgresql adapter constructor.
or
install postgres pdo /php pdo again.
I think that code is fine.
I was working a project where i had to use MySQL and PostGres.I connected my Postgresdb by following-
Below default 'db' connection string i wrote the following code inside services.php-
$di->setShared('pgphalcon', function () {
$config = $this->getConfig();
$adaptar = "PostgreSQL";
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adaptar;
$params = [
'host' => "localhost",
'port' => "5432", // for localhost no need to put this line unless u change the port
'username' => "postgres",
'password' => "12345",
'dbname' => "phalcon",
];
$connection = new $class($params);
return $connection;
});
Then your controller function
do the following-
public function yourcontrollernameAction()
{
$con = $this->di->get('pghalcon');
$post = $con->fetchOne("SELECT * FROM blog ORDER BY id DESC LIMIT 1", Phalcon\Db::FETCH_ASSOC);
$this->view->data= $post;
}
For better Understanding I'd like to suggest visit this url
One thing to clarify- I was failed to do db operation by Model for PostGreSQL. That's why I used this way.

how to connect to the database in zend with dsn line

zend framework 1.x
how to receive zend db_adapter and connect to mysql having dsn like
mysql://john:pass#localhost:3306/my_db
previously was always using this way:
$connectParams = array('dbname' => MY_DB,
'password' => MY_PASS,
'username' => MY_USER,
'host' => MY_HOST,
'slave' => array(),
'maxQueryAllowedTime' => 500,
'logQueries' => false);
return new \Db_Adapter_Pdo_Mysqlreplicator($connectParams);
sure i can parse dsn and use usual way, just curious if i can use DSN directly, coz PDO can use it but i cant find how to use it through Zend Db_Adapter
If you are using ZendFramework 1.x ->Try this:-
$config = array('dbname' => 'yourDbName',
'password' => 'yourpassword',
'username' => 'root',
'host' => 'localhost',
'slave' => array(),
'maxQueryAllowedTime' => 500,
'logQueries' => false);
$adapter = new Zend_Db_Adapter_Mysqli($config);
if ($config) {
echo ' connected';
}