Tag Archives: php

[Solved] PHP Error: ErrorException: preg_match(): Compilation failed: invalid range in character class at offset …

One day, the operator came over and said that it was useless to send the verification code by e-mail. As developers, it was our job to solve the problem.

First, check the error log and immediately find that PHP reported an error:

ErrorException: preg_match(): Compilation failed: invalid range in character class at offset 4

The error content means that the regular validation format is wrong and needs to be modified. However, we found no problems in the previous development and use, but recently.

This made us confused until we found that the PHP version of the server was changed from 7.2 to 7.3!

anger

After the difference, we found the cause of the problem:

Upgrade PHP from 7.2 to PHP 7.3

The writing of regular verification format is not standardized

The solution is also simple:

Upgrade the PHP Version (you can not do it, but it is recommended to keep it unified with the server)

Note whether special characters in regular format are escaped. For example, - in [] needs to be escaped: \-

[Solved] PHP connect to MongoDB error: Server does not support sessions

Solution steps:

Find the mongodb configuration file and make the following adjustments

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>

After restarting the mongodb service, enter Mongo for execution

rs.initiate()
    Done!

Reference:
https://stackoverflow.com/questions/50255195/how-to-configure-a-mongodb-cluster-which-supports-sessions

composer platform_check tp6 thinkphp6 laravel Error (PHP Version)

in fact, the reason should be that the new version of composer adds a thing to check the PHP version. You can solve the problem by turning off this setting

By command

composer config -l -g

Can find

platform_check

The value should be PHP_Only or something

Then turn it off by the command below

composer config -g platform-check false

Then again

composer update

It should be solved

Hyperf captures PHP error levels, such as notice, warning and error, and returns them to the front-end

1. register listener

Config/autoload/listeners.php is as follows

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
   \Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler::class
];

Hyperf\exceptionhandler\listener\errorexception handler:: class, error_reporting() Error level listener.

Refer to the listener chapter on the official website https://hyperf.wiki/2.2/#/zh -cn/exception-handler

2. Register global Middleware

Now configure the middleware under config/autoload/middleware.php

<?php

declare(strict_types=1);

namespace App\Middleware\Auth;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;

class FooMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;
    /**
     * @var HttpResponse
     */
    protected $response;

    public function __construct(ContainerInterface $container, HttpResponse $response)
    {
        $this->container = $container;
        $this->response = $response;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        try {
            return $handler->handle($request);
        } catch (\Throwable $throwable) {
            return $this->response->json(
                [
                    'code' => -1,
                    'message' => $throwable->getMessage(),
                    'data' => [
                    ],
                ]
            );
        }

    }
}

After completing the above steps, you can return PHP error prompts such as notice to the front end, and the PHP program will not continue to execute.

How to solve the error of PHP connection to mysql8

PHP version is 5.6. An error is reported when connecting to mysql8.0, but it is normal to connect to other versions before mysql8

The reason may be that mysql8 uses different password authentication methods by default, and mysql8.0 uses caching by default_sha2_Password, but previous versions used mysql_native_password

Solution:

Modify the/etc/my.cnf file

Find default authentication plugin = MySQL_native_Password line

Cancel the # number in front and restart mysql. MySQL will be used by default_native_Password, and then use the previous PHP script to test the connection to MySQL

As follows. The root user marked by the red arrow can connect normally

 

Pdostation:: bindparam vs bindvalue of PHP extension PDO MySQL

PDO statement:: bindparam vs bindvalue technology of PHP extending PDO MySQL

May yes published on November 16, 2015 13:27

Link to the original text: http://blog.lmlphp.com/archives/155/The_ difference_ of_ PDOStatement_ bindParam_ and_ bindValue_ of_ PHP_ extension_ PDO_ MySQL from lmlphp backyard

Lblog online experience site will be launched a few days ago http://lblog.lmlphp.com/ It’s been months since I moved to VPS. The version of PHP on the new VPS is relatively high, so when running, there will be an obsolete error of MySQL functions. The easiest way to deal with this error is to block it at the error reporting level, but this is not my style. I hope to solve it in a better way. But when I go to work, I don’t have time to write a class of mysqlpdo enhance, according to the style of MySQL driver class in lmlphp. For this reason, when writing PDO operation class, I specially defined an interface to restrict my behavior for fear of problems. After testing, it was proved that it was completely compatible with the previous MySQL class. It took so many months to finish writing, and I really couldn’t afford to hurt

This time when writing PDO driver class, I didn’t refer to other people’s writing method. I completely looked at the official documents, combined with my own needs, and tried to realize it as simple as possible. In fact, PDO is already an object-oriented style. In fact, it doesn’t need any driver class to encapsulate too much. It’s just written for better compatibility with the code in the project. At the beginning, when I looked at bindvalue and bindparam, the document gave me the feeling that one was a variable and the other was the exact value. It was only later found that one was a reference and the other was a common parameter. When testing the modification operation, it was found that the last field in the database was the same as the previous string, and the int type was not affected. Maybe my brain was too tired at that time. I worked out this problem for a long time. The next day I found out that it was caused by using bindparam in the loop

The mysqlpdoenhance driver class has been uploaded to lmlphp and lblog. Lblog has automatically selected the corresponding driver class when initializing the instance. This improvement makes the lblog system more excellent and more adaptable to the server environment

Mysqlpdoenhance class continues the simple style of MySQL class, with only one heavyweight query method to automatically determine whether to return resources or the number of rows affected. At the same time, we find that we can’t do this well when we use PDO. Because the number of rows is also affected when select is executed, which is different from common sense. People who understand databases should know that select does not affect rows, but the value returned by the rowcount method in PDO is the number of rows selected. Therefore, for this reason, we can only make a simple judgment from SQL, which is not perfect

An extract of query method is attached

public function query($sql, $params = array()){ $stmt = $this->db->prepare($sql, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); if($params){ foreach ($params as $k => $v){ if(is_array($v)){ $value = $v['value']; $type = isset($v['type']) ?$v['type'] : false; $length = isset($v['length']) ?$v['length'] : false; if($type && $length){ $stmt->bindValue($k, $value, $type, $length); }elseif($type){ $stmt->bindValue($k, $value, $type); }else{ $stmt->bindValue($k, $value); } }else{ $stmt->bindValue($k, $v); } } } $stmt->execute(); if(preg_match('/^update|^insert/i', trim($sql))){ return $stmt->rowCount(); }else{ return $stmt->fetchAll(PDO::FETCH_ASSOC); }}

Read (606) comments (0) view comments