Tag Archives: php

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

Error while sending query packet in PHP runtime

What are the eight life cycle hook functions of Vue>>>

question:

Error while sending query packet in PHP runtime

reason:

1

Run show variables like ‘max in MySQL_ allowed_ packet’;

View Max_ allowed_ Packet value, if Max_ allowed_ If the packet value is too low, the title error will be reported

solution:

Edit my.cnf (my.ini under Windows) and modify it in the [mysqld] section or the server configuration section of MySQL_ allowed_ Packet = 50m (or max_ allowed_ packet = 50 * 1024 * 1024)

command line modify set global Max_ allowed_ packet = 50 * 1024 * 1024

[Solved] PHP Fatal error: Class ‘mysqli’ not found in xxx

Fatal error: class’ mysqli ‘not found in xxx

The error indicates that PHP lacks mysqli extension. You can check the PHP compilation parameters first to confirm/usr/local/PHP5/bin/php-i|grepconfigure

Attached are other common software queries:

nginx:/usr/local/nginx/sbin/nginx-V

apache :cat/usr/local/apache/build/config.nice

mysql:cat/usr/local/mysql/bin/mysqlbug|grepconfigure

It’s true that we didn’t enter the source code package ext/mysqli later

Linux installation is compiled with configuration, but it disappears after we have installed PHP. There is a file called phpize in the command directory bin of PHP5 folder, which can be used to generate configuration

/Usr/local/PHP5/bin/phpize// enter here to run. After execution, the configure file appears

Now add the extension mysqli

./configure –prefix=/usr/local/mysqli –with-php-config=/usr/local/php5/bin/php-config –with-mysqli=/usr/local/mysql/bin/mysql_ config

make && amp; make test && amp; make install

After compilation:

Thank you for helping to make PHP better.
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files: /usr/local/php5/include/php/

The description is normal

Now mysqli. So appears

Modify php.ini file

extension_ Dir = “/ usr/local/PHP5/ext”
extension = mysqli. So new

I found that there are two PHP directories, which may be due to problems in the previous compilation, and both generated php.ini files. I found that they need to be modified to add the last two sentences to run normally

Finally, the generated mysqli.so file CP is put under/usr/local/PHP5/ext

Restart PHP FPM to complete the program

If you feel troublesome, you can directly delete the PHP file of compilation and installation, and the installation directory and configuration file can be recompiled and installed

If you have other questions, you can reply and learn together

Fatal error: class’ mysqli ‘not found in

/login/login_ action.html” method=”post” onSubmit=”return checkform(‘false’)”>

The php.ini file does not recognize the form and needs to be modified

Found

short_ open_ Change tag = on to on

[Solved] PHP write file permission failure: file_put_contents: failed to open stream: Permission denied

Questions

File is used in the method of writing log_ put_ Content this method, when executing the callback method to write the log today, prompts that there is no permission to write to the file, and reports an error

file_put_contents: failed to open stream: Permission denied

Solution process

Check the log folder permissions. Because it is generated on a daily basis, a scheduled task is executed on a regular basis, and the executing user is root. Therefore, both the generating folder user and the user group are root. In the callback method, the executing user is WWW, which is written to the log method

  if(!is_dir($dir)){
        mkdir($dir,0777,true);
    }

If the directory doesn’t exist, create a directory, but set 777 permission to create a folder in PHP’s MKDIR function. In fact, the created file still has 755 permission. In Linux system, there is a default permission when creating files/folders. This permission is affected by umask settings. We can find the following configuration in/etc/bashrc configuration file:

if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
        umask 002
else
        umask 022
fi

The default umask in Linux system is 022, which is similar to our 777 & amp; After the operation, it becomes 755. That’s why. The setting here directly affects the default permission setting of Linux system, not just the problem of PHP. Therefore, it is not recommended to change 022 to 000 directly

Final solution

First create a directory, and then use Chmod to modify the permission to 777

mkdir('test', 0777);
chmod('test', 0777);

What’s the difference between die() and exit() in PHP?

Knowledge map advanced must read: read how large-scale map data efficient storage and retrieval>>>

What is the difference between die() and exit() functions in PHP

I think the two have the same function, but I wonder if there are some differences between the two… What is this


#1st floor

As mentioned earlier, the two commands produce the same parser token

But

One small difference is how long it takes for the parser to return the token

I haven’t studied the PHP parser yet, but if it’s a long string of functions that start with “d” and a short string of functions that start with “e”, it’s going to take some time to find the function name of the function that starts with “.” e “. There may be other differences due to the way the entire function name is checked

I doubt it will be measurable unless you have a “perfect” environment dedicated to parsing PHP and many requests with different parameters. But there must be a difference. After all, PHP is an interpretive language


#2nd floor

There’s no difference – they’re the same

PHP Exit Manual:

Note: this language construction is equivalent to die()

PHP die Manual:

This language construction is equivalent to exit()


#3rd floor

They are essentially the same, although there are other suggestions in this paper


#4th floor

They sound similar, but exit () also allows you to set the exit code for the PHP script

Usually, you don’t actually need this script, but when writing a console PHP script, you may need to check with bash that the script does everything in the right way

You can then use exit () and capture later. Die() is not supported

Die() always exists as code 0. So essentially, the die() command does the following:

<?php
echo "I am going to die";
exit(0);
?>

Same as the following:

<?php
die("I am going to die");
?>

#5th floor

Mold in PHP manual:

Die – equivalent to exit

You can even die and exit same - with or without brackets

The only advantage of choosing die() instead of exit() may be that it saves time typing extra letters; -)

PHP use$_ SERVER[‘PHP_ Self ‘] to get the current page address and its security issues

Knowledge map advanced must read: read how large-scale map data efficient storage and retrieval>>>

PHP $_ SERVER[‘PHP_ SELF’]

$_ SERVER[‘PHP_ Self ‘] indicates the location address of the current PHP file relative to the root directory of the website, which is related to document root

Suppose we have the following website$_ SERVER[‘PHP_ The results of self ‘] are as follows:

http://www.ywp.com/php/ :/php/index.php
http://www.ywp.com/php/index.php :/php/index.php
http://www.ywp.com/php/index.php?test=foo :/php/index.php
http://www.ywp.com/php/index.php/test/foo :/php/index.php/test/foo

Therefore, it can be used$_ SERVER[‘PHP_ Self ‘] it’s very convenient to get the address of the current page:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

Taking the above address as an example, the results are as follows:

http://www.ywp.com/php/index.php

The above is a simple way to get the current page URL of the HTTP protocol. Just note that the address does not contain the requested parameter (?) in the URL?And the following string). If you want to get the full URL address that contains the request parameters, use$_ SERVER[‘REQUEST_ URI’] 。

PHP $_ SERVER[‘PHP_ Self ‘] security

Due to the use of$_ SERVER[‘PHP_ Self ‘] can easily obtain the current page address, so some programmers like to use the following method when submitting form data to the current page for processing:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Suppose the page address is:

http://www.ywp.com/php/index.php

The HTML code of the form is as follows:

<form method="post" action="/php/index.php">

This code is correct, but when the access address becomes:

http://www.ywp.com/php/index.php/test/foo

The page is executed normally, and the HTML code of the form becomes:

<form method="post" action="/php/index.php/test/foo">

Obviously, this code is not what we expected. Attackers can add attack code at will after the URL. To solve this problem, we can:

Using htmlentities ($_ SERVER[‘PHP_ Self ‘]$_ SERVER[‘PHP_ Self ‘], so that the possible malicious code in the URL can be converted into HTML code for display and cannot be executed

If possible, use$_ SERVER[‘SCRIPT_ [name ‘] or$_ SERVER[‘REQUEST_ Uri ‘] substitution$_ SERVER[‘PHP_ SELF’]

In the common code$_ SERVER[‘PHP_ Self ‘]:

$phpfile = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $phpfile)).$phpfile;