PHP mysqli Extensions

Source: Internet
Author: User
Tags php and mysql stmt

Mysqli is an object-oriented technology

using the Mysqli class

The Mysqli class object mainly controls the connection between PHP and MySQL servers, selects databases, sends SQL statements, and sets strings.

1. Connect to MySQL server

Method One:

$mysqli = new Mysqli ("localhost", "root", "1234567", "Bookstore");//Connect MySQL Database

The default database for changing the current connection can be changed by the select_db () method of the Mysqli object

Method Two:

$mysqli = new mysqli ();

$mysqli->connect ("localhost", "root", "1234567");

$mysqli->select_db ("bookstore");//Select a specific database

Method Three:

<?php

$mysqli = Mysqli_init ();//Create a Connection object

Setting connection options

$mysqli->options ();

$mysqli->real_connect ("localhost", "root", "1234567", "Bookstore");

?>

Handling connection Error reports

Mysqli_connect_errno ();//function test whether an error occurred during connection establishment

Mysqli_connect_error ();//function returns related error message

2. Turn off the connection to the MySQL server

$mysqli->close ();//

3. Execute SQL statements

The common method is query (); successful return True

The Affected_rows property of the Mysqli object gets how many records have changed

INSERT_ID (); Returns the automatically generated number

using the Mysqli_result class

The class object contains the result of a select query, and the member method that gets the data in the result set, the member property

The class object, by default, is returned by the SELECT statement executed by the query () method in the Mysqli object, and all the resulting data is retrieved from the MySQL server to the client, saved in the object

Creating a result Set object

Method One:

Call query ();

Cases:

$result = $mysqli->query ("SQL statement");//Return data to the client

$result = $mysqli->query ("SQL statement", mysqli_use_result);//left on MySQL server

Method Two: $mysqli->real_query ("SQL statement");//cannot determine the type of the returned result set $result = $mysqli->store_result ();//Get a buffered result set

Use the $result->close () method to reclaim the memory occupied by the result set


The result set object provides the Fetch_row () Fetch_assoc () Fetch_object () Fetch_array () method to read the data in the result set


Get column information

The Field_count property of the result set object gives the number of data columns

Current_field property gets the position that points to the forefront

Field_seek () method changes the offset position to the top of the column

Fetch_field () method returns information for the top of the column


Execute multiple SQL commands at once

Multiple SQL statements are executed using the Multi_query () method of the Mysqli object, and multiple SQL statements are written in a string separated by semicolons

Store_result ()//Retrieve all results from the client

More_result ()//check if there are other result sets

Next_result ()//Get Next result set

using the Mysqli_stmt class

The class object that defines and executes the parameterized SQL command

Get Preprocessing Statement Object

Method One:

Prepare the SQL statement to execute using the prepare () method in the Mysqli object to get a Mysqli_stmt object

Cases:

$stmt = $mysqli->prepare ("SQL statement"), each parameter of the//SQL statement is replaced with a placeholder, usually used?

Method Two:

$stmt = $mysqli->stmt_init ();//Get a Mysqli_stmt object

$stmt->prepare ("SQL statement");

Binding parameters using the Bind_param () method

The order of the binding parameters should correspond to the order of the parameters represented by the placeholders in the preprocessing statement

I represents all integer types

D denotes double and float types

s denotes all other types (including strings)

b indicates binary data type

Cases:

$stmt = $mysqli->prepare ($insertbook);
Binding variables
$stmt->bind_param (' SSD ', $bookName, $publisher, $price);
$bookName = "CSS";
$publisher = "Tsinghua University Press";
$price = ' 100 ';
$stmt->execute ();
Echo ' Number of rows inserted: '. $stmt->affected_rows. ' <br> ';
echo ' Auto-growing ID: '. $mysqli->insert_id. ' <br> ';

Execute () executes the prepared statement

Close () Recycle resources













PHP mysqli Extensions

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.