I am unable to get response from this code
if (!empty($data)) {
$json_response = json_encode($data);
echo $json_response;exit;
} else {
echo '';
}
here data is a coming from database in $data but not able to encode it.
$data containing following data
Array
(
[0] => Array
(
[doctor_id] => 1
[d_name] => Rahul Kenewadekar
[country] => India
[year] => DNB - 2012 to 2014
[profile_image] => Rahul_Kenewadekar-1439468566.jpg
[department] => Fellowship in MAS � DNB (Minimal Access Surgery -
Diplomate of National Board)
)
What is your database and table's encoding type(charset). Also you can check in app/config/database.php if the setting is set as below:
<?php
class DATABASE_CONFIG
{
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'XXXXXX',
'password' => 'XXXXXXXX',
'database' => 'XXXXXXXX',
'prefix' => '',
'encoding' => 'utf8'
);
You can also try doing "SET NAMES UTF8" before executing any query and just after doing connection to your DB.
Related
Situation: I need to get the parameters of the database from one PrestaShop 1.7, i know that are in parameters.php but i dont know how to get it in another .php.
The code of parameters.php are the following.
How i get the parameters??
Thanks.
<?php return array (
'parameters' =>
array (
'database_host' => '127.0.0.1',
'database_port' => '',
'database_name' => '',
'database_user' => '',
'database_password' => '',
'database_prefix' => 'ps_',
'database_engine' => 'InnoDB',
'mailer_transport' => 'smtp',
'mailer_host' => '127.0.0.1',
'mailer_user' => NULL,
'mailer_password' => NULL,
'secret' => 'fIz1vXGf14HQP0gYIGSOv3yqWAswhTLQ6JRAvCnAytNUUJgVOoHWYhip',
'ps_caching' => 'CacheMemcache',
'ps_cache_enable' => false,
'ps_creation_date' => '2017-10-09',
'locale' => 'es-ES',
'cookie_key' => '457GbAM3q5ctnvb0wjyyVcuNO0SfzFFxZghNSKW2TWhc9GfzPSvKDAQc',
'cookie_iv' => 'RvcXmr9J',
'new_cookie_key' => 'def00000ce15b57d4b2e1cdb214464708c3fefc4145607fff1400890a08aa234e5b5c7b0061a1af4647cbdf668ad9f383739c95f710f0e3d606d0e07dc11c20e42d6f0ec',
),
);
You can try this
$config = require _PS_CACHE_DIR_ . 'appParameters.php';
var_dump($config['parameters']['database_host']);
I tried to take out my database connection from the LocalConfiguration. But it doesn't work on this way. Do you have any ideas how i can realize it. Here what i tried to make it work:
LocalConfiguration.php:
<?php
include_once 'databaseConn.php';
return [
'BE' => [
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
],
and in the databaseConn.php:
<?php
$TYPO3_CONF_VARS['DB']['database'] = 'db_name';
$TYPO3_CONF_VARS['DB']['host'] = 'localhost';
$TYPO3_CONF_VARS['DB']['password'] = 'password';
$TYPO3_CONF_VARS['DB']['socket'] = '';
$TYPO3_CONF_VARS['DB']['username'] = 'usr_name';
Hope you can help me.
thanks
Chris
Create a file called AdditionalConfiguration.php in same directory. You can override every value there by addressing it directly
$GLOBALS['TYPO3_CONF_VARS']['DB']['database'] = 'custom';
You can also check the ApplicationContext by $context = GeneralUtility::getApplicationContext()->__toString(); which can be set in a .htaccess or vhost config
Use the following code in AdditionalConfiguration.php:
$configurationSettings = array();
#include_once(__DIR__.'/DatabaseCredentials.php');
#include_once(… some other files …);
if (is_array($configurationSettings)) {
foreach ($configurationSettings as $path => $value) {
$GLOBALS['TYPO3_CONF_VARS'] = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($GLOBALS['TYPO3_CONF_VARS'], $path, $value);
}
}
unset($configurationSettings);
then set your database credentials in DatabaseCredentials.php:
$configurationSettings = array_merge($configurationSettings, array(
'DB/database' => 'local_database',
'DB/username' => 'local_username',
'DB/password' => 'secret'
));
and you're done.
It is better that you add your database connection code into "LocalConfiguration.php".
return array(
'BE' => array(
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
),
'DB' => array(
'database' => 'db_name',
'extTablesDefinitionScript' => 'extTables.php',
'host' => 'localhost',
'password' => 'password',
'socket' => '',
'username' => 'username',
),
I have been playing with the developer preview version of CakePHP 3.0 and I'm stuck trying to get the new ORM working with pagination.
In my PostsController.php I have:
<?php
namespace App\Controller;
use App\Controller\AppController;
class PostsController extends AppController {
public $name = 'Posts';
public $uses = 'Posts';
public $components = ['Paginator'];
public $paginate = [
'fields' => ['Posts.id'],
'limit' => 1,
'order' => [
'Post.id' => 'asc'
]
];
public function index() {
$posts = $this->paginate('Posts');
$this->set('posts', $posts);
}
}
However the paging is working but the data doesn't come back. Apparently this because the data isn't directly returned in the new ORM but an object... Has anyone tried this yet? Knows how to fix the issue to get it working with the paginator?
I've been reading the Migration Guide: http://book.cakephp.org/3.0/en/appendices/orm-migration.html but don't see anything about combining it with the paginator.
Note: I can't debug $posts and show it here because it's about 2000 lines of code containing all sorts of stuff about the ORM. Here's a taster...
object(Cake\ORM\ResultSet) {
[protected] _query => object(Cake\ORM\Query) {
[protected] _table => object(Cake\ORM\Table) {
[protected] _table => 'posts'
[protected] _alias => 'Posts'
[protected] _connection => object(Cake\Database\Connection) {
[protected] _config => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'prefix' => '*****',
'persistent' => false,
'encoding' => 'utf8',
'name' => 'default',
'datasource' => object(Cake\Database\Driver\Mysql) {
[protected] _baseConfig => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'port' => '*****',
'persistent' => true,
'flags' => array(),
'encoding' => 'utf8',
'timezone' => null,
'init' => array(),
'dsn' => null
)
[protected] _config => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'port' => '*****',
'prefix' => '*****',
'persistent' => false,
'encoding' => 'utf8',
'name' => 'default',
'flags' => array(),
'timezone' => null,
'init' => array(),
'dsn' => null
)
[protected] _autoQuoting => false...
So as you can see it's a huge object and presumably the data is somewhere within it.
Apparently this because the data isn't directly returned in the new
ORM but an object...
It's not a bug it's a feature. ;) CakePHP3 returns a ResultSet object as you can see and entity objects for records. You'll have to work with these objects now instead of arrays.
I wounder if you really read the migration guide you've linked because it is all in there:
Cake\ORM\ResultSet - A collection of results that gives powerful tools for manipulating data in aggregate.
Cake\ORM\Entity - Represents a single row result. Makes accessing data and serializing to various formats a snap.
Further down on that page there is even more info about that. Take a look at the ResultSet API. You'll see that it implements Iterator, you can use it like an array:
Controller method:
public function index() {
$this->set('users', $this->Paginator->paginate($this->Users, [
'limit' => 5,
'conditions' => [
'Users.active' => 1
]
]));
}
There is a lot of documentation to read in the doc block of the paginate() method.
View index.ctp:
foreach ($users as $user) {
debug($user);
}
This will show you Entity objects. I'm not pasting the whole long debug output here, just a part of it.
object(Cake\ORM\Entity) {
[protected] _properties => array(
'password' => '*****',
'id' => '52892217-91ec-4e5d-a9f4-1b6cc0a8000a',
'username' => 'burzum',
'slug' => '',
// ...
To get something from the object back just do this:
echo $user->username;
The actual data is in the protected property Entity::$_properties and accessed by __get.
This will be in your controller.
public function index() {
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users'])
}
This you can put in your action
Paginatore logic
How to get all active connection when we use multiple databases?
I have tried:
foreach(Yii::app()->getComponents() as $component)
{
if ($component instanceof CDbConnection)
{
die(var_dump($component));
}
}
but it's look like database component not in Yii::app()->getComponents() result.
My db config:
'db'=>array(
'connectionString' => 'pgsql:host=127.0.0.1;port=yyyy;dbname=db1',
'emulatePrepare' => false,
'username' => 'user1',
'password' => 'pass1',
'schemaCachingDuration' => YII_DEBUG ? 0 : 86400000, // 1000 days
'enableParamLogging' => YII_DEBUG,
'charset' => 'utf8'
),
'db2'=>array(
'class' => 'CDbConnection',
'connectionString' => 'pgsql:host=xxx.xxx.xxx;port=xxxx;dbname=db2',
'emulatePrepare' => false,
'username' => 'user2',
'password' => 'pass2',
'schemaCachingDuration' => YII_DEBUG ? 0 : 86400000, // 1000 days
'enableParamLogging' => YII_DEBUG,
'charset' => 'utf8'
),
If you are in CActiveRecord instance ($model), you can use $model->getDbConnection(); or CActiveRecord::$db to get current connection database;
In class reference you can read:
public array getComponents(boolean $loadedOnly=true)
So, if you want to get every connection instead of only the actives:
foreach (Yii::app()->getComponents(false) as $component)
{
if (is_array($component) && $component['class'] == 'CDbConnection')
{
print_r($component);
}
}
Should list all
I've tried to do some tests, but cakephp doesn't create de test_{tablename} tables! He is trying to use de original tables.
Database config:
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'root',
'password' => '',
'database' => 'tests_clubpets',
'encoding' => 'utf8'
);
Fixture:
class AdminFixture extends CakeTestFixture {
var $name = 'Admin';
var $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
...
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);
var $records = array(
array(
'id' => 1,
'nome' => 'Lorem ipsum dolor sit amet',
...
);
}
Model:
class AdminTestCase extends CakeTestCase {
var $fixtures = array('app.admin');
function startTest() {
$this->Admin =& ClassRegistry::init('Admin');
}
function endTest() {
unset($this->Admin);
ClassRegistry::flush();
}
}
What can be wrong?
when you create the $test database connection, cake should be trying to make the tables in that database. according to your code that should be in tests_clubpets. make sure the database is created and that the user has permissions on the table. also check you did not make any typeo's
if you want to have your tables named test_{tablename}, use:
'prefix' => 'test_',