Simple operation of php and mysql

mysql is a fast, reliable and easy-to-use database system running on the server, which is often combined with php for web development.

There are two ways for php to connect to mysql:
mysql i ('i 'means improved): only for mysql
 pdo (php data objects): can be used in many databases

mysqli connection mode can be divided into object-oriented and process oriented, the basic form is similar, this paper only introduces object-oriented temporarily.

Connect and access the database

/*
 * $host: Host name or ip address
 * $username: mysql user name
 * $passwd: mysql Password
 * $dbname: Database name
 * $port: mysql Port number of the server
 * $socket: Specifies socket or named pipe to use
 */
//Open a new connection to the MySQL server
$con = new mysqli('localhost','root','','library');
$con->set_charset('utf-8');  //Set default client character set

//Returns the error description of the connection error, if any
if ($con->connect_error){  
    //Return error code of connection error
    echo $con->connect_errno;  
}else{
    //Connection successful
     /*
     * $con->host_info: Return MySQL server hostname and connection type
     * $con->client_info: Return to MySQL client library version
     * $con->client_version: Return MySQL client library version as an integer
     * $con->server_info: Return to MySQL server version
     * $con->server_version: Return MySQL server version as an integer
     *
     * $con->affected_rows: Returns the number of record lines affected by the previous MySQL operation
     * $con->field_count: Returns the number of columns most recently queried
     *
     * Reference: http://www.runoob.com/php/php-ref-mysqli.html
     */

    $sql = 'select * from book';

    //For a successful SELECT, SHOW, DESCRIBE, or EXPLAIN query, a mysqli "result object is returned.
    //TRUE will be returned for other successful queries. If it fails, FALSE is returned.
    $result = $con->query($sql);

    if ($result->num_rows){
       //Operate on results
    }

    //Close a previously open database connection.
    $con->close();  
}

Interpretation of result set
Through $result = $con - > query ($SQL), put the result in the $result variable. The explanation about $result is as follows:

/*
 * $result All four result sets return query results to an array or object.
 * The first call returns the first line, after which each call pointer moves down one line
 *
 * Reference: http://blog.csdn.net/vezn_king/article/details/51703256
 */

//fetch_object(): get the current row from the result set and return it as an object. If there is an error, return false
$row1 = $result->fetch_object();
echo $row1->name.'<br/>';

//fetch_array(): get a row from the result set as an associated array, or a number array, or both. If there is an error, return false
//This function is an extended version of fetch row(), but it is more efficient than fetch row()
$row2 = $result->fetch_array();
echo $row2['name'].'<br/>';
echo $row2[0].'<br/>';

//Fetch? Assoc(): get a row from the result set and return it as an associated array. If there is an error, return false
$row3 = $result->fetch_assoc();
echo $row3['name'].'<br/>';

//>Fetch row(): get a row from the result set and return it as an index array. If there is an error, return false
$row4 = $result->fetch_row();
echo $row4[1].'<br/>';

Keywords: MySQL PHP Database SQL

Added by hightechredneck on Sat, 02 May 2020 00:52:55 +0300