Tag Archives: php

[Solved] yii error: Setting unknown property: yii\console\Request: :cookieValidationKey

1. Command:
php yii
2. Error:
Exception ‘yii\base\UnknownPropertyException’ with message ‘Setting unknown property: yii\console\Request::cookieValidationKey’
3. Reason:
console/yii file

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/config/main.php',
    require __DIR__ . '/../conf/main-local.php'
);

$application = new yii\console\Application($config);

The above program execution entry combines all configurations before actual operation, and some configuration parameters are invalid for console operation

4. Solution:
Delete these parameters before performing console operations

unset($config['components']['request']);
$application = new yii\console\Application($config);

[Solved] Artisan error: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

Problem Description:

php artisan migrate Error:

Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_em
ail_unique`(`email`))

Cause Analysis:
The maximum character length of utf8 encoding supported by MySql is 3 bytes, if a wide character of 4 bytes is encountered, an insertion exception will occur. The maximum Unicode character that can be encoded by three bytes UTF-8 is 0xffff, which is the basic multiliterate plane (BMP) in Unicode. Therefore, Unicode characters that are not in the Basic Multicultural Plane, including Emoji emojis (Emoji is a special Unicode encoding), cannot be stored using MySql’s utf8 character set.

This should also be one of the reasons why Laravel 5.4 switched to the 4-byte length utf8mb4 character encoding. However, it should be noted that the utf8mb4 character encoding is only supported from MySql version 5.5.3 onwards (check the version: selection version();). If the MySql version is too low, a version update is required.

Solution:
1. Upgrade MySql version to 5.5.3 or higher. Add in /app/providers/AppServiceProvider.php:

use Illuminate\Support\Facades\Schema;

public function boot()
    {
        Schema::defaultStringLength(191);
    }

2. Delete the table in the database and re execute php artisan migrate

Tp6 Cannot enable error [How to Solve]

tp6 open error, you need to copy the root directory of .example.env, save it as .env, then modify the header to APP_DEBUG=true, and then directory config/app.php modify ‘show_error_msg’ => fales.

If the pdo still reports an error after starting php.ini again, log out the .env file

[DATABASE]
TYPE=mysql
HOSTNAME=host.docker.internal
DATABASE=easyadmin
USERNAME=root
PASSWORD=root
HOSTPORT=3306
CHARSET=utf8
DEBUG=true
PREFIX=ea_

 

[Solved] PHPMailer Error: SMTP ERROR: Failed to connect to server: (0)

1. Explain

After configured $mail->Username$mail->password, the error is reported as follows:

2021-09-01 12:22:37 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Error

2. Solution

1. Ignore SSL authentication (this is my reason)

Add the following code after code: $mail = new PHPMailer();

// Instantiating the PHPMailer core class
$mail = new PHPMailer(); 

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

...

2. Set the port to bit 25, do not use SSL, 465 (other possible reasons)

Change

// Set up ssl encryption for login authentication
$mail->SMTPSecure = 'ssl';
// set the remote server port number for ssl connection to smtp server
$mail->Port = 465;

to

$mail->Port = 25;

[Solved] PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive nodes

The environment background is docker DNMP installation of ES + PHP

Corresponding docker compose configuration and corresponding port

Port 9200 can also be accessed normally

Code

Then 500 errors are reported, including:

 PHP Fatal error:  Uncaught Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive nodes found in your cluster in /www/localhost/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php:67
Stack trace:
#0 /www/localhost/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(87): Elasticsearch\ConnectionPool\StaticNoPingConnectionPool->nextConnection()
#1 /www/localhost/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(105): Elasticsearch\Transport->getConnection()
#2 /www/localhost/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(275): Elasticsearch\Transport->performRequest('POST', '/my_index/my_ty...', Array, '{"testField":"a...', Array)
#3 /www/localhost/vendor/react/promise/src/FulfilledPromise.php(28): Elasticsearch\Connections\Connection->Elasticsearch\Connections\{closure}(Array)
#4 /www/localhost/vendor/ezimuel/ringphp/src/Future/CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(Object(Clos in /www/localhost/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php on line 67

The reason is that the node cannot be found. The solution is as follows: find your own IP and specify the IP

Then the execution is OK

[Solved] Error while sending STMT_PREPARE packet. PID=29294

In the error reporting scenario, the command command of tp6 + nohup of Linux is used to execute the continuous script
error reason: the connection to the database will be disconnected for a long time

Detailed error information is as follows

[think\exception\ErrorException]                    
Error while sending STMT_PREPARE packet. PID=29294  
                                                      


PHP Fatal error:  Uncaught think\exception\ErrorException: Error while sending STMT_CLOSE packet. PID=29294 in /....../vendor/topthink/think-orm/src/db/PDOConnection.php:603
Stack trace:
#0 /....../vendor/topthink/think-orm/src/db/PDOConnection.php(603): think\initializer\Error->appError(2, 'Error while sen...', '/...', 603, Array)
#1 /....../topthink/think-orm/src/db/PDOConnection.php(1576): think\db\PDOConnection->free()
#2 /....../vendor/topthink/think-orm/src/db/Connection.php(345): think\db\PDOConnection->close()
#3 [internal function]: think\db\Connection->__destruct()
#4 {main}
thrown in /....../vendor/topthink/think-orm/src/db/PDOConnection.php on line 603

Solution:

Modify the database configuration file database.php and set it to true to enable disconnect and reconnect
 // whether to disconnect and reconnect
'break_reconnect' => true,

[Solved] PHP post Datas json_decode Error: 4 JSON_ERROR_SYNTAX

Error description

In the PHP development process, when processing json strings, json_decode returns NULL, calling last_error returns 4 (JSON_ERROR_SYNTAX), but json strings can be correctly processed by other languages ​​such as python, javascript or some online json parsers.

Diagnosis

There are several situations that generally cause php json_decode errors here:

1. The json string is read from a file and the character order mark (BOM) is not removed

2. json contains invisible characters, json_decode parsing errors

3. json object value is a single-quoted string

In particular, the third error is relatively hidden, the naked eye is often easy to ignore

Solution:

The following solutions are given for the above three cases

1.BOM Issue:

Open the file in binary mode and confirm whether there is a BOM. If so, remove the BOM before parsing. The following code takes UTF-8 as an example to detect and delete BOM.

function removeBOM($data) {

if (0 === strpos(bin2hex($data), ‘efbbbf’)) {

return substr($data, 3);

}

return $data;

}

2.Invisible character

Remove invisible characters before parsing.

for ($i = 0; $i <= 31; ++$i) {

$s = str_ replace(chr($i), “”, $s);

}

3.Single quote string value

Let’s look at the following example:

<?php

$s = “{\”x\”:’abcde’}”;

$j = json_ decode($s, true);

var_ dump($j);

echo json_ last_ error() . “\n”;

PHP 5.5. 9 output

NULL

four

Generally, you only need to replace single quotation marks with double quotation marks. During specific processing, you should pay attention to that single quotation marks may also appear in other places. Whether to replace them globally needs to be analyzed according to the specific situation.

[Solved] PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;

php artisan migrate Run Error:
PDOException:: (“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes”)

Solution:
Add the following codes in the file of app/Providers/AppSeviceProvider.php:
use Illuminate\Support\Facades\Schema;
Add in the method of foot:
Schema::defaultStringLength(191);

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
		Schema::defaultStringLength(191);
    }
}

After the prompt
pdoexception:: (“sqlstate [42s01]: base table or view already exists: 1050 table ‘users’ already exists”)
, delete the user table and re-run PHP artist migrate

[Solved] PostgreSQL configure: error: readline library not found

preface

An error occurs when installing PostgreSQL, as shown below

configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.

Solution:

Check whether the readLine package is installed in the system

rpm -qa | grep readline

Install readline-devel pack

yum -y install -y readline-devel

Execute configure again successfully

The explanation of readLine comes from the official website

--without-readline
Prevents use of the Readline library (and libedit as well). Thisoption disables command-line
editing and history in psql, so it is notrecommended.

Note: when you execute configure, you can add “– without readLine” to avoid this error, but PostgreSQL officials do not recommend it