PHP5 working with MySQL database Base code _php Tutorial

Source: Internet
Author: User
1. Establishing a database connection
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "" "," mydb ");
?>

Establishing a database connection requires four parameters, namely the database address, the database access user name, the database access password, and the database name. In addition to using the Mysqli object's construction method above to establish a database connection, you can also call its Connect method to establish a connection to the database.
Copy CodeThe code is as follows:
$mysqli = new mysqli ();
$mysqli->connect ("localhost", "root", "" "," mydb ");
?>

You can also establish a data connection through the Mysqli object's construction method, specifying the database to access through the select_db method.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
?>

The error number of the current connection is obtained by the errno property of the Mysqli object, and the error number is returned as 0 if there are no errors for the current connection.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
}
Else
{
echo "The Connection is error!";
Exit ();
}
?>

Of course, you can get the error message for the current connection through the error property of the Mysqli object, or return "" If there is no error.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "rootsss", "" ");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

2. Querying the database
The query database can use the Mysqli object's Query method, which returns the result set of the query database.
The syntax is: $MYSQLI->query (query statement, query mode);
There are two types of query modes:
①mysqli_store_result. Returns the result as a cache set, which means that the entire result set can be navigated immediately. This setting is the default setting. When the result set is queried, it is put into memory, which means that if the amount of data in the result set is large, it will consume more memory. But in this way we can easily tell how many rows a query has returned or want to jump to a row in the result set immediately.
②mysqli_use_result. Returns the result set as a non-cached set. This means that the result set is fetched from the database server as needed, which can improve performance for large result set data. However, there are many restrictions on the operation of the result set, such as the number of rows to be fetched.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
$sql = "SELECT * from student";
$result = $mysqli->query ($sql);
echo "Result row nums:". $result->num_rows. "
"; Show number of result sets
Iterate result Sets
while (list ($id, $name, $age, $address) = $result->fetch_row ())
{
echo "$id: $name: $age: $address". "
";
}
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

Use the Fetch_row method of the result set object to get each row of data in the result sets, each row of data is an associative array, and each of these data is output using the list method. You can also output each row in the result set by using the output object.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
$sql = "SELECT * from student";
$result = $mysqli->query ($sql);
echo "Result row nums:". $result->num_rows. "
"; Show number of result sets
Iterate result Sets
while ($rowObject = $result->fetch_object ())
{
echo "$rowObject->id: $rowObject->name: $rowObject->age: $rowObject->address". "
";
}
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

It uses the Fetch_object method to encapsulate the data in each row as an object, in the example above, the object is $rowobject, and each column in the database becomes the property of that object, and the corresponding field value can be obtained by invoking its property name. For example, get student name $rowobject->name.
You can also use the Fetch_array method to return each row of data as an associative array or an indexed array, or to return both an associative array and an indexed array. The mode parameter of the Fetch_array method specifies the pattern of the current returned array:
①mysqli_assoc. Returns the associative array, key is the field name, and value is the field value.
②mysqli_num. Returns an array of indexes with the same order of return and query fields.
③mysqli_both. Also returns an associative array and an indexed array. Default settings.
Copy CodeThe code is as follows:
while ($row = $result->fetch_array (MYSQLI_ASSOC))//return associative array
{
echo $row [' ID ']. $row [' name ']. $row [' age ']. $row [' address ']. "
";
}
?>

Or
Copy CodeThe code is as follows:
while ($row = $result->fetch_array (mysqli_num))//returns an indexed array
{
echo $row [0]. $row [1]. $row [2]. $row [3]. "
";
}
?>

3. Freeing up memory
If the amount of data in the result set is large and has been used, the free method of the result set object is used to release the memory occupied by the result set. Once the free method is called, the result set is no longer available.
Copy CodeThe code is as follows:
...
$result->free (); Freeing memory
?>

4. Add, modify, and delete operations
The query method using the Mysqli object can still be used to add, modify, and delete the database, except that it is a different SQL statement. Let's take the example of adding data:
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
$sql = "INSERT into student (id,name,age,address) VALUES (' 8 ', ' Kay ', ' All ', ' Xian ')";
$result = $mysqli->query ($sql);
Echo $mysqli->affected_rows; The number of rows affected by the output
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

Call the Affected_rows property of the Mysqli object to get the number of rows affected.
5. Close the database connection
When a database connection is finished, call the Close method of the Mysqli object to close it.
Copy CodeThe code is as follows:
...
$mysqli->close ();
?>

6. Using Binding parameters
The binding parameters in PHP are the same as preprocessing SQL in Java, and when a SQL is executed repeatedly, the parameters of SQL can be used to increase the speed of SQL execution.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
$sql = "INSERT into student (id,name,age,address) VALUES (?,?,?,?)";
$stmt = $mysqli->stmt_init (); To create a preprocessing object
$stmt->prepare ($sql); Preprocessing SQL
$stmt->bind_param ("ISIS", $id, $name, $age, $address); Set the bound variable the first parameter is the data type of the variable
for ($i = n, $i <100; $i + +)
{
$id = $i + 1;
$name = "Kaikai";
$age = 23;
$address = "Xian";
$stmt->execute (); Execute SQL statement
}
Echo $mysqli->affected_rows; The number of rows affected by the output
$stmt->close (); Frees memory consumed by preprocessed objects
$mysqli->close (); To close a database connection
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

It is important to note that the first parameter of the Bind_param method, which specifies the data type of the following variables, is as follows:
①i: All the integer types.
②d: All double and float types.
The ③b:blob type.
④s: Other data types include strings.
7. Result binding
The result binding is used to bind the query results with some variables.
Copy CodeThe code is as follows:
$mysqli = new Mysqli ("localhost", "root", "");
$mysqli->select_db ("MyDB");
if ($mysqli->errno = = 0)//Determine if the current connection is successful
{
$sql = "SELECT * from student";
$stmt = $mysqli->stmt_init (); To create a preprocessing object
$stmt->prepare ($sql); Preprocessing SQL
$stmt->bind_result ($id, $name, $age, $address); Bind a query result field to a variable
$stmt->execute (); Execute SQL statement
The while ($stmt->fetch ())//fetch method is used to get each row in the result set and assigns the corresponding field value to the variable
{
echo "$id: $name: $age: $address". "
";
}
$stmt->close (); Frees memory consumed by preprocessed objects
$mysqli->close (); To close a database connection
}
Else
{
Echo $mysqli->error; Output current error message
Exit ();
}
?>

http://www.bkjia.com/PHPjc/320663.html www.bkjia.com true http://www.bkjia.com/PHPjc/320663.html techarticle 1. Establish the database connection copy code code as follows: PHP $mysqli = new mysqli ("localhost", "root", "" "," mydb "); Create a database connection requires four parameters, respectively, the database address ...

  • 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.