Tag Archives: Mysqli

php mysqli_ connect_ Error () and mysqli_ connect_ The difference of errno() function

First let’s look at the official definition:

Grammatical definitions:
Definition:
> Mysqli_connect_error () : The function returns an error description of the last connection error.
> Mysqli_connect_errno () : The function returns the error code for the last connection error.

Note: One returns an error description and the other an error code.

A PHP Error was encountered Severity: Warning Message: mysqli::real_connect(): Headers and client

When I upgraded MySQL, the PHP linked database reported an error. After checking the data, I found that the MySQL version was changed, which was inconsistent with the previously compiled version, so the error was reported. The solution was to recompile mySQLND, my MySQL and PHP installed by YUM were very simple.

killall php-fpm
yum remove php70w-mysql
yum install php70w-mysqlnd

After installation, just restart PHP-FPM!

PHP error in Windows: class’ mysqli ‘not found

For example, we have access to mysql through the API provided by PHP extension Mysqli. The error report is: Class ‘mysqli’ not found. The reason for this error is that PHP was unable to load the dynamic library php_mysqli.dll. The solution is to configure the relevant variables so that the program finds the location.
By default php_mysqli. DLL is existing in the PHP installation directory under ext directory:

The first thing we need to do is to modify the PHP configuration file. The configuration file in Windows is in php.ini under the PHP installation directory. If not, you can make a copy through PHp.ini-Development. We found one of them; Extension =php_mysqli.dll, remove the semicolon at the beginning to allow the configuration to take effect. This means that you can use the MySQli extension.

In general, this is fine, but our configuration file PHp.ini will not take effect if our system does not configure the environment variable PHPRC. In order for php.ini to take effect, we need to configure the environment variable PHPRC, which specifies the PHP installation directory.

After configuring the environment variables, we can restart the Apache server. We can verify our PHP-related information by using phpInfo (). First, the Configuration File is in the Loaded Configuration File, not “none”, but php.ini under the PHP installation directory.

The second is that the mySQli extension appears in the middle of the page. If one of the above two steps is missing, this part will not be displayed. When we call the MYSQli API in our PHP code, we will report an error:
said that php_mysqli.dll from ext in the PHP installation directory should be copied to C:\Windows\ System32. This is because php.ini has not taken effect, and php.ini should be copied to C:\Windows directory.
Next, we can query the emP table ID and name in the mysql database test through a program, the code is as follows:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "root";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connect Fail " . $conn->connect_error);
}

$sql = "SELECT id, name FROM emp";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 output";
}
$conn->close();

We visited the PHP file on the page and got the following results:

A simple mysqli application and the problem of mysqli extension are solved.

PHP Error Object of class mysqli could not be converted to string in

Write a PHP encountered an error: the Object of the class mysqli could not be converted to a string
An object of class mysqli_result cannot be converted to a string
Check the code:

    function query($sql){
        echo $sql;
        $query = mysqli_query(self::$con,$sql)or die(mysqli_error(self::$con));
    
        return $query;
    }

Mysqli_result is returned by mysqli_query, so the error should be on the return value.

You cannot directly output an object as a string in PHP5 above.
So just change the type of the return value.

    function query($sql){
        echo $sql;
        $query = mysqli_query(self::$con,$sql)or die(mysqli_error(self::$con));
        return  mysqli_fetch_array($query);
        //return $query;
    }

Returns as an array with the mysqli_fetch_array() function.