ZF2- Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' (PHP 5.5.12) - php

I am working on a zend framework 2 application, and I am working with port 8080. My main probleme in the begining is that i couldn't retrieve or add data from database in the views (probably, a probleme with connection with database). It didn't show any error, but data couldn't be retrieved or added.
I tried to specify the port in global.php:
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=pizza;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'
)
),
);
After that, I got this error:
Warning: PDO::__construct(): MySQL server has gone away in
I looked for a solution, and I found one. I commented a line in php.ini:
extension=php_pdo_mysql.dll
It solved the probleme, but now Im stuck with another probleme:
Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in C:\wamp\www\pizza\config\autoload\global.php on line 18
I found some suggestions on how to fix this probleme by Linux command:
apt-get install php5-mysql /etc/init.d/apache2 restart
But i want a solution for windows 7, or any other you may find useful.
Thanks for your time.

Unfortunately, it seems like you found bad advice on two occasions. You're getting the Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' error because by commenting out php_pdo_mysql.dll you effectively disabled the MySQL extension. So uncomment that line - you definitely want that there.
You mentioned that you're "working with port 8080" but it's not clear if your ZF2 application is running on port 8080, or if you're running MySQL on that port (which would be non-standard). You said you tried to specify this port in your global.php configuration file, but the port is nowhere to be seen in the configuration you posted.
The "MySQL server has gone away" error means the MySQL connection is timing out. Usually this is caused by PHP scripts that run for a very long time (>30 seconds) without touching the DB connection. This is unlikely to happen in a standard web application, so we'd need more info about what led up to this error in order to help you.
You also said that you "couldn't retrieve or add data from database in the views". If this is still the case, there must be an error somewhere, even if it's not being shown to you. So check the relevant error logs and your error settings.

Related

Gii Model Generator generates Database Exception (I use SQL Server)

I have a Yii2 framework connected to an SQL Server 2012 database.
I have already configured the config/db.php file as follows:
return [
'class' => 'yii\db\Connection',
'dsn' => 'sqlsrv:Server=localhost;Database=Evaluators;MultipleActiveResultSets=true',
'username' => '_myUsername_',
'password' => '_myPassword_',
'charset' => 'utf8',
];
I have also installed the necessary extension files in the /ext directory.
I am using SQL Server 2012 instead of MySql.
When I try to Start Gii Model Generator I get the following error :
Database Exception – yii\db\Exception could not find driver
Caused by: PDOException could not find driver
Any ideas what should I change or do?
The issue can be from your php configuration.
In that case, I solved it by doing the following: uncomment extension=php_pdo_mysql.dll in your php.ini. (I am on MySQL instead of SQL server 2012)
More explanations here: https://www.jeffgeerling.com/blog/2018/installing-php-7-and-composer-on-windows-10
Since your using SQL server 2012, I cannot confirm for sure, but you should investigate this: https://www.php.net/manual/en/ref.pdo-sqlsrv.php
Good luck

Yii - CDbHttpSession, session table before login

I follow this tutorial to create my user online list.
In my local MAMP server on Mac OSX it work fine, but if I tried to put in Ubuntu server, I got this error (at the index, without logged in):
Fatal error: Uncaught exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'session' already exists.
I can catch the exception but I would to know because in OSX it works fine but in Ubuntu it's not works.
I think it may be something in Apache or other config: is it possible ?
Is session created before login ? The component need a user_id (not null) in the session table so I think there is anything I don't understand.
In both php.ini I set session.auto_start = 0.
Probably the problem is that in those 2 servers you have slight differences, and among them the table 'session' which is already available in the Ubuntu server, and is probably handled for a total different reason against what you need, while in your MAC env there is no such thing as 'session' table.
The easy and clean way I can think of to solve the collision of names is to rename your table name from 'session' to 'mysession' changing the config setting this way:
'session' => array (
'class' => 'application.components.DbHttpSession',
'connectionID' => 'db',
'sessionTableName' => 'mysession',
'userTableName' => 'user'
),
And update every occurrence of the table name (like in queries) in your code.
This way the collision should be gone and everything should work fine.
I hope it helps.

Why does yii2 try to connect to mysql when I have not configured it to?

This is what I did:
I downloaded the yii2 advanced template.
Ran php init.
Configured each of main-local.php files in
environments/dev/common/config
environments/prod/common/config
common/config
I added the following to the 'components'
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:/path/to/sqlitedbs/yayr.sq3',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
I configured the apache vhosts as explained in the readme.
Running yii migrate created the sqlite database file in the configured location with no errors.
What breaks is when I go to the frontend app and try to submit the sign up form, it gives me this error:
Exception
Database Exception – yii\db\Exception
SQLSTATE[28000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
↵
Caused by: PDOException
SQLSTATE[28000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
in /home/johnsmith/dev/test/yayr/vendor/yiisoft/yii2/db/Connection.php at line 579
The full stack trace can be seen: http://pastebin.com/KEKH1zbM
I have used my IDE to search for every location that a mysql dsn exists. The only place is in /tests/codeception/config/config.php which should not be called by submitting a form. To double check, I commented out the config, and it didn't help.
I also tried setting 'driverName' => 'sqlite', in the config, but that did not make any difference. Neither did commenting out the username, password, or charset parameters.
None of the functions referenced by the stack trace appear to manually call MySQL. They all look for the configured dsn.
So, why does Yii think it can even try to connect to MySQL? Doesn't ActiveRecord abstract out all the SQL so that it won't matter if I'm using MySQL or sqlite?
How do I get past this issue? Using sqlite would make initial development a lot easier.
Thanks!
Basics of the Yii2 advanced template
You have the environments-folder containing you actual config files
The frontend- and backend-folders hold copies of those "local"-files, depending on which environment you selected, they get copied from dev or prod
Now what does that mean? If you perform changes on ANY file within the envirnments folder, you have to re-init your application. You simply do this by opening a console, navigate to the base folder of your application and call the init-command. You will then be asked which envirnment you want and what files to overwrite.
After that you should see your changes beeing reflected in the frontend- and backend-folder. The idea behind this is to prevent if/else-structures within the config files and have a clean application. By the way: the index.php within the web-folders gets copied the same way. That way you could even customize this...and therefore the way the application gets loaded initially.
Back to your problem
For me it looks like you changed the settings within the environments/.../local-...-configfile, which is right...but then didn't re-init the application. That way the config file within the front- and backend still contain your old settings.
Documantation of the advanced template
Everything is very well documented here:
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md
After init command
Create a new database and add it in common/config/main-local.php
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=your-db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
Apply yii migrate in console command!
It'll create the tables for the application to work

CakePHP Database connection "Mysql" is missing, or could not be created

There have been several other posts about this, but none of the answers seemed to work for me.
When I navigate to the CakePHP page on my local machine, there is one error:
Cake is NOT able to connect to the database. Database connection
"Mysql" is missing, or could not be created.
When I run this helpful code in my home.ctp, I get the following response:
Error!: SQLSTATE[42000] [1049] Unknown database 'test'
However, my Users/Ben/Sites/myapp/app/Config/database.php looks like this (I set MAMP to look for the document root in Users/Ben/Sites):
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'Ben',
'password' => 'mypass',
'database' => 'CV',
);
}
I have created a mysql user called Ben with password mypass and created a database called CV under that. Moreover, I can't find mention of a test database anywhere. Help?
Try adding the socket:
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
An alternative to unix_socket (especially for OS X people) is to replace localhost with 127.0.0.1
Would be as Follows :
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'user',
'password' => 'password',
'database' => 'database-name',
'prefix' => '',
'encoding' => 'utf8',
);
Edit php.ini and add:
extension=php_pdo_mysql.dll
Then restart your web server
On Mac, using MAMP as a development platform, for cake the correct solution is using Domingo Casarrubio solution.
Add the unix_socket parameter to your database configurations.
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
This error can also be caused if your connecting database user doesn't have the proper privileges. I believe you only need a minimum of INSERT, SELECT, UPDATE, and DELETE.
Always check username/password and the user privileges first since CakePHP will most likely give a vague database connection error for either.
I noticed that you've had asked this an year ago, and most probably would've solved this by now. However, for those facing the same issues when attempting to install CakePHP on XAMPP, all you have to do is change the 'login' to 'root', i.e. the default login of XAMPP, and leave the 'password' as '', i.e. blank. The complete code in your database.php file should look like this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'ckblog',//replace with your own database name
'prefix' => '',
//'encoding' => 'utf8',
);
That's it.
I had the same problem and found out eventually that it was caused by CakePhp not accepting that I used a user with a password, even if that user was created in PHPMyAdmin. I had to use the user 'root' with no password.
I found this out after making the following change to the file /lib/Cake/Error/exceptions.php.
The original line:
protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';
is changed into this instead (note the change from single to double quotes):
protected $_messageTemplate = "Database connection \"%s\" is missing, or could not be created:\n %s";
This will give you the reason for the problem so that you may change the cause properly.
I have had this problem since upgrading to OSX Yosemite and inserting following line did the trick for me:
'unix_socket' => '/tmp/mysql.sock'
It can be that mysql PDO support is missing.
as root (or using sudo):
apt-get install php5-mysql
Just to help Ubuntu users out:
I had the same error in my ubuntu 13.10 machine with the newest xampp downlaoded directly from apachefriends. Tried most of the stuff in every post I could find about this error, but not the mac-specific stuff.
In the end, the fix happened to be the same as the elected answer here:
Find the socket that mysqld creates for programs to connect to:
user#host /opt$ find . -name mysql.sock
/opt/lampp/var/mysql/mysql.sock
add it to your cakePHP database configuration file (cakePHP)/app/Config/database.php
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
To me, this finally resulted in my cake commands being able to be executed without the "Error: Database connection "Mysql" is missing, or could not be created.".
Because, cake bake use unix socket for connecting to database
so that you need add unix_socket for connection string.
You have to confirm location that store mysql.sock in WAS
Example: in my case i'm using xampp on MACOS 10.11
(edit file Config/database.php)
public $default = array(
‘datasource’ => ‘Database/Mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘login’ => ‘root’,
‘password’ => ‘root’,
‘database’ => ‘cakephp’,
‘encoding’ => ‘utf8’,
‘unix_socket’ => ‘/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock’
);
Finally, It's work for me!
What did it for me in the end was that I had created a table in my database, but there was no data in it.
In order for CakePHP to recognize the MySql connection, there has to be a table with data in it.
You might need to create the table in your php file... Open up phpMyAdmin and check to ensure that they database CV exists.
It's your model. Open that up and there must be the following line
public $useDbConfig = 'local';
This overwrites global config & set it back to local
I tried splicing the code from Example 2 of http://php.net/manual/en/pdo.connections.php into /app/View/Pages/home.ctp. I had to fix the arguments the PDO constructor and change the name of the table in the query. The example 2 code returned the error "Error!: could not find driver". Based on King Jk's answer I was attempting to modify the php.ini when I started to wonder where a php_pdo_mysql.so might live. http://php.net/pdo_mysql showed how it was compiled as part of PHP via the --with-pdo-mysql option to configure. Recompiling fixed my problem. Note I'm working on a Ubuntu 12.10 system with PHP 5.5.9 and Apache Webserver 2.4.6
In my case it was because the database didn't exist. I expected running ./app/Console/cake schema create would create it but it did not. Creating it with create database <database name> in mysql did the trick (although I had already assigned privileges).
I've been struggling with this the whole weekend and finally solved it. Turns out that the php.ini is pointing to a non-existing "extensions dir". Create a phpinfo() file and look at the value of this field:
I noticed that in the mamp php installed folder there is a no-debug-non-zts-20131226 folder, which is different from the value shown in the phpinfo(). What I did was to clone this folder and changed the name to the value of the phpinfo(). Probably you could modify the php.ini file but I didn't want to.
I don't know if you solved your problem, but I'm posting this because my problem was different and google took me here, so I hope to help future googlers having a similiar issue.
Hope this helps.
If you're on Godaddy (or any other shared hosting for that matter), they may be limiting outgoing connections to ports 80 and 443 only.
System configuration:
Fedora 32
php-fpm 7.4.13
mariadb 10.4.17
CAKE 2.10.17
Error message from CAKE:
Database connection "Mysql" is missing, or could not be created.
Enhanced error message using answer at https://stackoverflow.com/a/24722976/5025060
Database connection "Mysql" is missing, or could not be created: Selected driver is not enabled
My problem was no "connector" between PHP and SQL was installed. The solution was:
dnf install php-mysqlnd
This allowed PHP to connect to the database as specified in CAKE's database.php configuration file.

Cake is NOT able to connect to the database

I am using Wamp server and I'm trying to install CakePHP 2.0.0 but I'm having trouble with it.
I put the CakePHP 2.0.0 files in my wamp server folder "www" and then "cake" folder.
When I enter address http://localhost/cake in my browser then following message is displayed:
CakePHP: the rapid development php framework
Release Notes for CakePHP 2.0.0-dev.
Notice (1024): Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your application [CORE\Cake\Utility\Debugger.php, line 647]
Notice (1024): Please change the value of 'Security.cipherSeed' in app/config/core.php to a numeric (digits only) seed value specific to your application [CORE\Cake\Utility\Debugger.php, line 651]
Your tmp directory is writable.
The FileEngine is being used for caching. To change the config edit APP/config/core.php
Your database configuration file is present.
Cake is NOT able to connect to the database.
Editing this Page
To change the content of this page, create: APP/views/pages/home.ctp.
To change its layout, create: APP/views/layouts/default.ctp.
You can also add some CSS styles for your pages at: APP/webroot/css.
I had the same problem and it took a lot of researching to determine the actual problem.
The new version of CakePHP uses pdo to establish a connection rather than mysql or mysqli as it did previously. As you are using a Windows environment, just enable the following in php.ini file.
extension=php_pdo_mysql.dll
NOTHING to do with using root and I also found it an real annoyance when trying to move from 1.3 to 2.0
As for the arrogant answer from deceze, I found NO mention of this change anywhere on the CakePHP download / install / docs.
I can't upvote due to a lack of reputation, however I'd like to point out that despite the comments under your question (which, in part, are correct), George Wood is exactly correct: you need to enable
extension=php_pdo_mysql.dll (Windows)
or for me it was extension=pdo_mysql.so (Arch Linux)
...and there may be some variants out there too. I've just struggled with this for an hour. Best of luck with your coding efforts.
My problem with cakephp was solved on Ubuntu by doing this:
sudo apt-get install php5-mysql
sudo /etc/init.d/apache2 restart
I'm using Ubuntu 10.04, MySQL, Apache2 and PHP5.
Once I did that I could use the MySQL user I created. So it wasn't a cakephp config error it was not knowing php5-mysql was not installed by default.
They are NOT errors, they are notices. This means you can do some configurations in order to make it work. Read over them and do what they tell you to do. Seems like errors happen in your config/, watch out to provide correct information such as database's name and password.
Edit
Just to answer your question, CakePHP is a PHP framework that assist you with constructing your website in MVC model. Instead writing source code from scratch, by using framework, you can inherit all its functionality that speed up your development time, help you to deal with more-complex structure/procedures more easily.
If you haven't still solved the connection issue with database, try doing a sanity check on these bits..
'driver' (ok in 1.3) has been renamed to 'datasource'
'mysql' (ok in 1.3) has to be specified as 'Database/Mysql'
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'sandbox',
'prefix' => '',
'encoding' => 'utf8');
Let me know if this worked.
(I was trying to reuse my old 1.3 config and got caught in the same loop.)
Please find solution as shown below.
Notice (1024): Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your application [CORE\Cake\Utility\Debugger.php, line 647]
Solution: Go thorough this file app/config/core.php
find Security.salt and change it to any value.
Notice (1024): Please change the value of 'Security.cipherSeed' in app/config/core.php to a numeric (digits only) seed value specific to your application [CORE\Cake\Utility\Debugger.php, line 651]
Solution: Go thorough this file app/config/core.php
find Security.cipherSeed and change it to any value.
Your tmp directory is writable.
Solution: Give proper permission for this path app/tmp
Cake is NOT able to connect to the database.
Solution: Go thorough this file app/config/database.php
set proper parameters for database connection.
Just adding this in here... I came across this (and other similar solutions) but only recently found the problem. I had an issue with my custom Apache build due to other software issues and decided to quickly get a working environment rather than reformat my PC.
I first installed Apache 2.4 and PHP 5.4. These versions were unfortunately incompatible with most of my code, and again, I wanted to quickly get a working environment, so I uninstalled and went back to WAMP with Apache 2.2 and PHP 5.3.
At some point, WAMPServer was looking at C:\Program Files (x86)\PHP\php.ini for my php.ini file. I do not know how or why it was looking there. Unfortunately I was editing C:\wamp\bin\php\php5.3.13\php.ini instead. I didn't realize why MySQL wasn't working until I loaded up a call to phpinfo() and noticed PHP was looking in the "wrong" place for my php.ini.
In case anyone else has a similar issue, I figured I'd add it here to the mix. Good luck!
I had the same problem and I figured it out.
I had the wrong username and password for database connection.
So I opened database and edited the database.php file. Now cake is able to connect to the database.
Recently I had the same problem with xampp installed on a windows machine, for me it was because when entering the details
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
windows could not resolve localhost to being 127.0.0.1, this can be solved by one of either solutions:
Change details in app/Config/database.php to
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
Or edit in windows C:\Windows\System32\drivers\etc\hosts (You may need to run Nopepad.exe as administrator to do this)
add a line at the end with:
127.0.0.1 localhost
I solved this error by downgrading my xampp from version 7.0 to version 5.6. Seems that the updated xampp doesn't support the cakeframework perfectly. I used this trick to also succesfully install orangescrum.
On local host for mysql, it is compulsory to use the user "root" rather than any other user. Hope this helps.
Also check this.

Categories