PHP中用于数据库操作的10个函数
1. mysqli_connect()
The mysqli_connect() function is used to create a connection to a MySQL database. It takes four parameters: the host name, username, password and database name. It returns a mysqli object that can be used for subsequent database queries.
Example:
$conn = mysqli_connect("localhost", "myusername", "mypassword", "mydatabase");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
2. mysqli_query()
The mysqli_query() function is used to execute a database query. It takes two parameters: the mysqli object returned by mysqli_connect(), and the SQL query to execute. It returns a mysqli_result object that can be used to fetch the results of the query.
Example:
$result = mysqli_query($conn, "SELECT * FROM mytable");
if (!$result) {
die("Query failed: " . mysqli_error($conn));
}
3. mysqli_num_rows()
The mysqli_num_rows() function is used to get the number of rows returned by a database query. It takes the mysqli_result object returned by mysqli_query() as its parameter.
Example:
$result = mysqli_query($conn, "SELECT * FROM mytable"); $num_rows = mysqli_num_rows($result); echo "Number of rows returned: " . $num_rows;
4. mysqli_fetch_assoc()
The mysqli_fetch_assoc() function is used to fetch a single row of results from a database query as an associative array. It takes the mysqli_result object returned by mysqli_query() as its parameter.
Example:
$result = mysqli_query($conn, "SELECT * FROM mytable"); $row = mysqli_fetch_assoc($result); echo "Name: " . $row["name"] . ", Age: " . $row["age"];
5. mysqli_fetch_array()
The mysqli_fetch_array() function is used to fetch a single row of results from a database query as both an associative array and a numeric array. It takes the mysqli_result object returned by mysqli_query() as its parameter, and optionally a constant that determines the type of array to return.
Example:
$result = mysqli_query($conn, "SELECT * FROM mytable"); $row = mysqli_fetch_array($result, MYSQLI_BOTH); echo "Name: " . $row["name"] . ", Age: " . $row[1];
6. mysqli_real_escape_string()
The mysqli_real_escape_string() function is used to escape special characters in a string for use in a SQL statement. It takes the mysqli object returned by mysqli_connect() as its first parameter, and the string to escape as its second parameter.
Example:
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$sql = "INSERT INTO mytable (name) VALUES ('$name')";
mysqli_query($conn, $sql);
7. mysqli_insert_id()
The mysqli_insert_id() function is used to get the auto-generated ID of the last inserted row in a table. It takes the mysqli object returned by mysqli_connect() as its parameter.
Example:
$sql = "INSERT INTO mytable (name) VALUES ('John')";
mysqli_query($conn, $sql);
$id = mysqli_insert_id($conn);
echo "Inserted row with ID: " . $id;
8. mysqli_close()
The mysqli_close() function is used to close a database connection opened with mysqli_connect(). It takes the mysqli object returned by mysqli_connect() as its parameter.
Example:
mysqli_close($conn);
9. mysqli_fetch_object()
The mysqli_fetch_object() function is used to fetch a single row of results from a database query as an object. It takes the mysqli_result object returned by mysqli_query() as its parameter.
Example:
$result = mysqli_query($conn, "SELECT * FROM mytable"); $row = mysqli_fetch_object($result); echo "Name: " . $row->name . ", Age: " . $row->age;
10. mysqli_error()
The mysqli_error() function is used to get the error message for the last executed database query. It takes the mysqli object returned by mysqli_connect() as its parameter.
Example:
$result = mysqli_query($conn, "SELECT * FROM non_existent_table");
if (!$result) {
echo "Error: " . mysqli_error($conn);
}
