Problems loading Zends Session in bootstrap - php

I am having trouble getting Zend to store my session in MySQL table. I have followed the Zend Framework guide step by step, and am not sure if is where am putting the code in my bootstrap file, but once the code snippet is in place and I load my site Apache just crashes. Literally crashes. My logs don't say anyhing.
Here is my code:
$db = Zend_Db::factory( 'Pdo_Mysql', array(
'host' => 'localhost',
'username' => 'root',
'password' => '*****',
'dbname' => 'drecords'
));
Zend_Db_Table_Abstract::setDefaultAdapter( $db );
$config = array(
'name' => 'sessions',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setSaveHandler( new Zend_Session_SaveHandler_DbTable( $config ) );
//start your session!
Zend_Session::start();
I am running this code right after at the end of my Bootstrap file.
My question is what am I doing wrong if am following Zends documentation? Is there something I need to know like an extra configuration option in my MySQL or PHP.ini that am not aware of?

Did you create the table in MySQL?
your user have insert/update/delete privileges on the table
do your php setups output errors, what environment are your running production/development
I think the code should probably output some error but if your disabled the output of those you cannot see them.

Related

How can I move my codigniter databse to google cloud sql?

I want to move my codeigniter project database to google cloud sql but I didn't see phpmyadmin.
How can i export my database to google and where do i get myconnection details, host,password,user and dbname just like I have on my local server?
The documentation is not very clear to someone who is not familiar with that environment. Thanks
This is my database config file
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '104.197.147.157',
'username' => 'mylasttestingaccount',
'password' => '',
'database' => 'mydatabase',
/*
'username' => 'develope_prakash',
'password' => '[REDACTED]',
'database' => 'develope_ubah',
*/
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
This is a multipart question, so I'll answer the questions as I understand them.
NOTE: Cloud SQL instances are not managed using phpmyadmin, but they are managed mainly using the Cloud Console. This also means that whatever changes you want to make (such as adding users) needs to be done using the Cloud Console, or from one of the other management tools such as the gcloud command line tool.
1) You'll need to create an instance first. I highly advise a 2nd gen instance.
2) Adding a user can be done from the Console or the gcloud command line tool. Both are described in the same article.
3) As for importing data from another database, there's a separate article on this. There's some conditions that need to be met before you can successfully import data. The export needs to be created in a certain way. This is described in detail in this article. After getting the export you can import it using the steps described in the first article I linked in this paragraph.
To wrap it up, I highly advise you to read through the documentation about Cloud SQL. Any of the ones I linked have a list of articles on the left side which contains just about everything you need to know about Cloud SQL.
One additional note, NEVER provide passwords for anything on a public site like SO.

What is the correct way to apply the yii2 cache?

As I'm new to the cache mechanism, I gone through the yii2 documentation. As per the documentation, I added the below config in db.php in yii2 basic application.
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db_new',
'username' => 'root',
'password' => 'root123',
'charset' => 'utf8',
'enableQueryCache' => true,
'queryCacheDuration' => 86400,
'enableSchemaCache' => true,
// Name of the cache component used to store schema information
'schemaCache' => 'cache',
// Duration of schema cache.
'schemaCacheDuration' => 86400, // 24H it is in seconds
];
also I added the cache component in web.php
'components' => [
'cache' => [
'class' => 'yii\caching\ApcCache'
]
]
And added the below code while retrieving a record from clients table.
$db = Clients::getDb();
$client = $db->cache(function ($db)use($id) {
return Clients::find()->where(['id' => $id])->all();
});
I assume my client table one record ex)$id = 3 is cached. So next time if I try to retrieve same record from clients table it will pull from cache not from scratch.
My questions are
What I did above is this correct or anything I need to configure
more?
Where it is storing in the local system.
Thanks in advance.
what you did is correct for query caching, found in data caching here.
there are more caching mechanisms available, like fragment, page or http caching
as far as i know, the place where the cached data is stored depends on the caching component. apc stores in memory
yii's FileCache will store files under /runtime
good to know you can flush caches with yii's console command yii cache/flush-all

Codeigniter: using a database to store database names for multiple users

Using Codeigniter 3.0.6
Until now I have been using the standard setup for Codeigniter. I have a database and all connects just fine. The question that I'm having trouble with is that I want to add a 'global_accounts' database, from which, depending on the user currently logged in will choose the database to load.
Global_Accounts
--------------------------------------------------------
user | coolUser | user2
pass | coolPassword | passy
url | coolestsite.mysite.com | u2.mysite.com
database_name | coolestsite_application_db | u2_application_db
The application data/schema doesn't really matter as far as I'm concerned. What I want (and am going to set up, but outside of the scope for this question) is for when a user registers, they will fill out user, pass and url, and db_name will be automatically created. The script will then create a new database with that name, creating a blank copy of the application db for the user.
So for now, we're hard coding that, so we can assume that it's working perfectly.
What I want is inside Codeigniter's database.php file:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'coolsite_application_db'
...etc
);
Instead of hard coding this file, I want to be able to do something like this:
$db['global'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'global_accounts'
...
);
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => $this->db['global']->get_db_name($loggedInUserId);
...etc
);
Hopefully that makes sense - I need to get the db name based on the logged in user. Obviously I need to build either a Model or just a function to grab it, but the question is where? Do I add a model, connect to the global db, and then load the model in the database config? Is there some other, easier, more productive way to do this, or is that it? Will the codeigniter core files be able to be called from inside the codeigniter config files?
Use 2 database connections. 1 for the global user accounts and 1 for the actual user database. Then create a helper function to grab the user databases. Just an idea.

ZF2 PDO error when loading Action from console

I have an application that works correctly when accessed from a browser, I can query PostgreSQL without any problems but when I try to load it via the console I get the error:
Zend\Db\Adapter\Exception\RuntimeException
Connect Error: could not find driver
The routing, controller and action load correctly as long as I don't try to query the database.
Is there anything I'm missing, maybe something else I need to set when trying to query the DB from the console?
From the console I run it via php /web/public/index.php action which works until I add the DB calls.
//UPDATE
My global.php looks like:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'pgsql:dbname=myDB;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
for brevity;
After discussion on chat, turns out server was configured to handle http and console requests over different PHP binary's. Changing the php path solved the problem.

Migrating existing cakephp 3.0 database to postgres from mysql

I am in the process of migrating a cakephp 3.0 database from mysql to postgress. I used this tool for the database migration and it worked beautifully. After that I changed the config file as shown below.
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'username' => 'postgres',
'password' => 'mypass',
'database' => 'cake_bookmarks',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
The root folder in localhost also shows "successfully connected to database". However when i run my application, it shows an error:
Cannot describe mytable. It has 0 columns. Cake\Database\Exception
I can't make sure if this is because of not connecting to the database (which i think is unlikely as the root page shows as connected) or cakephp being unable to use my database.
If so, how can I fix the issue. I am quite new to cakephp too, just confguring and doing basic stuff.
Try the following (test after each step):
Check if the table is present in the database
Check if the expected columns are defined into the table
Clear the Cake cache (if is FileCache is enough to delete files under tmp/cache/persistent tmp/cache/models and tmp/cache/views
Check the permissions of the specific user on the database cake_bookmarks (maybe via phppgadmin)
Hope to help!

Categories