Function name: mysqli_stmt::fetch()
Applicable version: PHP 5, PHP 7
Usage: This function is used to get the next line in the result set from the prepared statement. It will return a boolean value indicating whether the next row was successfully retrieved.
Syntax: mysqli_stmt::fetch(int $resulttype = MYSQLI_BOTH) : mixed
parameter:
Return value: If the next row is successfully obtained, an array or NULL will be returned. Return NULL if there are no more lines available.
Example:
// 假设已经建立了数据库连接,并且准备好了一个语句对象$stmt // 执行语句$stmt->execute(); // 绑定结果集$stmt->bind_result($column1, $column2); // 获取结果集的下一行数据while ($stmt->fetch()) { echo "Column 1: " . $column1 . "<br>"; echo "Column 2: " . $column2 . "<br>"; } // 关闭语句$stmt->close();
In the above example, we first execute the execution() method of the prepared statement object $stmt to execute the statement. Then, use the bind_result() method to bind the columns in the result set to the variables $column1 and $column2. Next, call the fetch() method loop to get each row of data in the result set and output it to the screen.
Note that the fetch() method stores the next row of data of the result set in the bound variable, and will automatically move the result set pointer to the next row every time the loop iteration. When there are no more rows available, the fetch() method will return NULL and the loop will end. Finally, we close the statement object $stmt.
The above example is only used to demonstrate the basic usage of the fetch() method, and appropriate adjustments may be required in actual applications according to specific needs.