In fact, there is no desire today. -mysqli, Desire. -mysqli_php Tutorials

Source: Internet
Author: User

In fact, there is no desire today. -mysqli, Desire. -mysqli


Hi

Noon Shuangshuang Swim for a while, but the afternoon to a punch Superman read, unexpectedly sprout not out of the desire to learn ... Force yourself to update something, read a book.

1, Mysqli

Second, Mysqli OOP-based programming

2.1 Use Resolution

-Basic

Mysqli is an extended class library, which is essentially a class (? )。

The general process is the same as MySQL: connection, library selection, character set, SQL statement execution, close connection .

--Link Library Example

/*
* Connect and select library
*/
$mysqli =new mysqli (' localhost ', ' root ', ');
Print_r ($MYSQLI); echo "
";

echo $mysqli->select_db(' Test ');
";

$mysqli 2=new mysqli ();
Print_r ($mysqli 2->connect(' localhost ', ' root ', ') '), echo "
";

Print_r ($mysqli 3=new mysqli (' localhost ', ' root ', ', ' test '), echo "
";

Three different methods, the method is to use the Mysqli class properties to do, of course, you can also use the mysqli command to link;

$con =mysqli_connect (Host,username,password)

There is some information in the results

Mysqli Object
(
[Affected_rows] = 0
[Client_info] = Mysqlnd 5.0.11-dev-20120503-$Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $
[Client_version] = 50011
[Connect_errno] = 0
[Connect_error] =
[errno] = 0
[ERROR] =
[Error_list] = = Array
(
)
[Field_count] = 0
[Host_info] = localhost via TCP/IP
[INFO] =
[insert_id] = 0
[Server_info] = 5.6.17
[Server_version] = 50617
[Stat] = uptime:968 threads:1 questions:24 Slow queries:0 opens:70 Flush tables:1 Open tables:63 queries per se Cond avg:0.024
[SQLState] = 00000
[Protocol_version] = 10
[THREAD_ID] = 11
[Warning_count] = 0
)

These properties can be obtained through the properties of the object, such as

echo $mysqli->client_info;echo "
";

or by a corresponding method, these things can be found in the manual.

Header (' Content-type:text/html;charset=utf-8 ');
1. Establish a connection to MySQL data
$mysqli =new mysqli (' localhost ', ' root ', ' root ');
Print_r ($MYSQLI);
2. Open the specified database
$mysqli->select_db (' Test ');
$mysqli =new mysqli ();
$mysqli->connect (' 127.0.0.1 ', ' root ', ' root ');
Print_r ($MYSQLI);

Open the specified database while establishing the connection
$mysqli = @new mysqli (' localhost ', ' root ', ' root ', ' test ');
Print_r ($MYSQLI);
$mysqli->connect_errno: Get the error number of the connection generated
$mysqli->connect_error: Get the error message generated by the connection
if ($mysqli->connect_errno) {
Die (' Connect Error: '. $mysqli->connect_error);
}
Print_r ($MYSQLI);
Echo '

';
echo ' Client information: '. $mysqli->client_info. '
';
echo $mysqli->get_client_info (). '
';
Echo ' client version: '. $mysqli->client_version. '
';
Echo ' ';
Echo ' Server-side information: '. $mysqli->server_info. '
';
echo $mysqli->get_server_info ();
Echo ' ';
Echo ' Server version: '. $mysqli->server_version. '
';

Echo '

';

--Character Set example

1. Establish a connection to MySQL
$mysqli = @new mysqli (' localhost ', ' root ', ' root ', ' test ');
if ($mysqli->connect_errno) {
Die (' Connect Error: '. $mysqli->connect_error);
}
2. Set the default client encoding method UTF8
$mysqli->set_charset (' UTF8 ');

3. Execute SQL query
$sql =<<<>
CREATE TABLE IF not EXISTS mysqli (
ID TINYINT UNSIGNED auto_increment KEY,
Username VARCHAR () not NULL
);
EOF;
$res = $mysqli->query ($sql);
Var_dump ($res);

/*
Select/desc/describe/show/explain execution successfully Returns Mysqli_result object, execution failure returns false
For execution of other SQL statements, the execution returns true successfully, otherwise false
*/
Close connection
$mysqli->close ();

It is important to note that the database is UTF8, not utf-8;

2.2 Inserting record operations

Increase.

--connect.php

Because a series of operations connected to a database are common, the easiest way to do this is to encapsulate it and call it everywhere

Require_once ' connect.php ';

connect.php

/*
* Connect and select Library (header) files
*/
$mysqli =new mysqli (' localhost ', ' root ', ' ', ' test ');
if ($mysqli->connect_errno) {
Die (' Connect Error: '. $mysqli->connect_error);
}else{
echo ' Client information: '. $mysqli->client_info. '
';
}
$mysqli->set_charset (' UTF8 ');

-Increase

/*
* Database Insert Data
*/

Require_once ' connect.php ';

$sql = "Insert mysqli (username) value (' Tom ')";
echo $mysqli->query ($sql);

Here, a single SQL statement is executed.

Or refine a point, add a judgment, and output the error message.

if ($res) {
Echo $mysqli->insert_id;
}else{
Echo ' ERROR '. $mysqli->error;
}

Or, insert more than one record

$sql = "Insert mysqli (username) value (' Sdaf '), (' Andy ')";

2.3 Update History

Update.

$sql = "Update test set ID=ID+10";
$mysqli->query ($sql);

2.4 Delete

By deleting

$sql = "Delete from mysqli where id>=2";

--

In particular, there are three cases of affected_rows return:

-1 There is a problem with the SQL statement;

0 no affected statements;

>=0 the number of affected bars.

--Summary

Header (' Content-type:text/html;charset=utf-8 ');
$mysqli =new mysqli (' localhost ', ' root ', ' root ', ' test ');
if ($mysqli->connect_errno) {
Die (' CONNECT ERROR: '. $mysqli->connect_error);
}
$mysqli->set_charset (' UTF8 ');

Execute SQL query
Add a record
Execute a single SQL statement and execute only one SQL statement
$sql = "INSERT User (Username,password) VALUES (' King ', ' King ');";
$sql. = "DROP TABLE user;";
$sql = "INSERT User (Username,password) VALUES (' Queen1 ', ' Queen1 '), (' Queen2 ', ' Queen2 '), (' Queen3 ', ' Queen3 '), (' Queen4 ') , ' Queen4 ') ";
$res = $mysqli->query ($sql);
if ($res) {
Gets the value of the auto_increment generated by the previous insert operation
Echo ' Congratulations on your successful registration, you are the website section '. $mysqli->insert_id. ' Users
';
Get the number of affected record strips from the previous operation
Echo ' has '. $mysqli->affected_rows. ' records are affected ';
}else{
Get the error number and error message from the previous action
Echo ' ERROR '. $mysqli->errno. ': ' $mysqli->error;
}
Echo '

';

The age of +10 in the table
$sql = "UPDATE user SET age=age+10";
$res = $mysqli->query ($sql);
if ($res) {
echo $mysqli->affected_rows. ' records are updated ';
}else{
echo "ERROR". $mysqli->errno. ': ' $mysqli->error;
}
Echo '

';

Remove the id<=6 user from the table
$sql = "DELETE from user WHERE id<=6";
$res = $mysqli->query ($sql);
if ($res) {
echo $mysqli->affected_rows. ' records are deleted ';
}else{
echo "ERROR". $mysqli->errno. ': ' $mysqli->error;
}
Close the connection to MySQL
$mysqli->close ();

2.5 check

It is important to note that a select is used, so the result set returned is a print_r or var_dump that can be printed out.

So here's the choice of the result set that's going to be returned.

Header (' Content-type:text/html;charset=utf-8 ');
$mysqli =new mysqli (' localhost ', ' root ', ' root ', ' test ');
if ($mysqli->connect_errno) {
Die (' CONNECT ERROR: '. $mysqli->connect_error);
}
$mysqli->set_charset (' UTF8 ');
$sql = "Select id,username,age from User";
$mysqli _result= $mysqli->query ($sql);
Var_dump ($mysqli _result);
if ($mysqli _result && $mysqli _result->num_rows>0) {
Echo $mysqli _result->num_rows;
$rows = $mysqli _result->fetch_all ();//Gets all the records in the result set, which is returned by default to two-dimensional
The form of Index + index
$rows = $mysqli _result->fetch_all (mysqli_num);
$rows = $mysqli _result->fetch_all (MYSQLI_ASSOC);
$rows = $mysqli _result->fetch_all (mysqli_both);
$row = $mysqli _result->fetch_row ();//Get result set a record returned as an indexed array
Print_r ($row);
Echo '

';
//$row = $mysqli _result->fetch_assoc ();//Get a record in the result set as an associative array return
Print_r ($row);
Echo ' ';
//$row = $mysqli _result->fetch_array (); //both
Print_r ($row);

Echo ' ';
//$row = $mysqli _result->fetch_array (MYSQLI_ASSOC);
Print_r ($row);

Echo ' ';
//$row = $mysqli _result->fetch_object ();
Print_r ($row);
Echo ' ';
Move the inner pointer of the result set
$mysqli _result->data_seek (0);
$row = $mysqli _result->fetch_assoc ();
Print_r ($row);

Print_r ($rows);

while ($row = $mysqli _result->fetch_assoc ()) {
Print_r ($row);
Echo '

';
$rows []= $row;
}
Print_r ($rows);

//Release result set
$mysqli _result->free ();


}else{
Echo ' query error or no record in result set ';
}
$mysqli->close ();

http://www.bkjia.com/PHPjc/1079207.html www.bkjia.com true http://www.bkjia.com/PHPjc/1079207.html techarticle In fact, there is no desire today. -mysqli, Desire. -mysqli Hi Noon Shuangshuang swim for a while, but the afternoon to a punch Superman read, unexpectedly sprout not to learn the desire ... Force yourself ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.