Current Location: Home> Function Categories> mysql_unbuffered_query

mysql_unbuffered_query

Send a SQL query to MySQL (no fetch/cache results).
Name:mysql_unbuffered_query
Category:Uncategorized
Programming Language:php
One-line Description:Execute an SQL query without buffering and return a result resource, which can be used to obtain query results row by row

Function name: mysql_unbuffered_query()

Applicable version: PHP 5.0.0 - PHP 5.6.x

Usage: The mysql_unbuffered_query() function executes an SQL query without buffering and returns a result resource, which can be used to obtain query results row by row. This function is suitable for processing large amounts of data because it does not load the query results into memory at once, but instead gets the results line by line, thereby reducing memory consumption.

Syntax: resource mysql_unbuffered_query ( string $query [, resource $link_identifier = NULL ] )

parameter:

  • query: Required, SQL query statement to be executed.
  • link_identifier: Optional, MySQL connection identifier. If not provided, the function tries to find the previously opened connection.

Return value: If the query is executed successfully, the result resource will be returned, otherwise false will be returned.

Example:

<?php // 创建数据库连接 $link = mysql_connect("localhost", "username", "password"); if (!$link) { die("连接数据库失败: " . mysql_error()); } // 选择数据库 $db_selected = mysql_select_db("mydatabase", $link); if (!$db_selected) { die("选择数据库失败: " . mysql_error()); } // 执行不带缓冲的查询 $result = mysql_unbuffered_query("SELECT * FROM mytable", $link); if (!$result) { die("查询失败: " . mysql_error()); } // 逐行获取查询结果 while ($row = mysql_fetch_assoc($result)) { echo "ID: " . $row['id'] . ", Name: " . $row['name'] . "
"; } // Release the result resource mysql_free_result($result); // Close the database connection mysql_close($link); ?>

In the above example, we first create the database connection and then select the database to use. Next, we use the mysql_unbuffered_query() function to execute a query without buffering, and the query returns a result resource. We obtain the query results line by line through the mysql_fetch_assoc() function, and print out the ID and Name of each line. Finally, we freed the result resource and closed the database connection.

Note that after PHP 7.0.0, the mysql_unbuffered_query() function has been deprecated and has been removed after PHP 7.4.0. Instead, the mysqli_unbuffered_query() function and the PDO::query() method. If you are using PHP 7 or later, it is recommended to use the new MySQL extension or PDO to perform unbuffered queries.

Similar Functions
Popular Articles