Get result from php

mysqli_stmt_get_result

Только для процедурного стиля: Идентификатор выражения, полученный с помощью mysqli_stmt_init() .

Возвращаемые значения

Возвращает результат или FALSE в случае возникновения ошибки.

Только для MySQL Native Driver

Доступно только с расширением mysqlnd.

Примеры

Пример #1 Объектно-ориентированный стиль

$mysqli = new mysqli ( «127.0.0.1» , «user» , «password» , «world» );

if( $mysqli -> connect_error )
die( » $mysqli -> connect_errno : $mysqli -> connect_error » );
>

$query = «SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1» ;

$stmt = $mysqli -> stmt_init ();
if(! $stmt -> prepare ( $query ))
print «Ошибка подготовки запроса\n» ;
>
else
$stmt -> bind_param ( «s» , $continent );

$continent_array = array( ‘Europe’ , ‘Africa’ , ‘Asia’ , ‘North America’ );

foreach( $continent_array as $continent )
$stmt -> execute ();
$result = $stmt -> get_result ();
while ( $row = $result -> fetch_array ( MYSQLI_NUM ))
foreach ( $row as $r )
print » $r » ;
>
print «\n» ;
>
>
>

$stmt -> close ();
$mysqli -> close ();
?>

Пример #2 Процедурный стиль

$link = mysqli_connect ( «127.0.0.1» , «user» , «password» , «world» );

if (! $link )
$error = mysqli_connect_error ();
$errno = mysqli_connect_errno ();
print » $errno : $error \n» ;
exit();
>

$query = «SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1» ;

$stmt = mysqli_stmt_init ( $link );
if(! mysqli_stmt_prepare ( $stmt , $query ))
print «Ошибка подготовки запроса\n» ;
>
else
mysqli_stmt_bind_param ( $stmt , «s» , $continent );

$continent_array = array( ‘Europe’ , ‘Africa’ , ‘Asia’ , ‘North America’ );

foreach( $continent_array as $continent )
mysqli_stmt_execute ( $stmt );
$result = mysqli_stmt_get_result ( $stmt );
while ( $row = mysqli_fetch_array ( $result , MYSQLI_NUM ))
foreach ( $row as $r )
print » $r » ;
>
print «\n» ;
>
>
>
mysqli_stmt_close ( $stmt );
mysqli_close ( $link );
?>

Результат выполнения данных примеров:

Albania 3401200 Europe Algeria 31471000 Africa Afghanistan 22720000 Asia Anguilla 8000 North America

Смотрите также

  • mysqli_prepare() — Подготавливает SQL выражение к выполнению
  • mysqli_stmt_result_metadata() — Возвращает метаданные результирующей таблицы подготавливаемого запроса
  • mysqli_stmt_fetch() — Связывает результаты подготовленного выражения с переменными
  • mysqli_fetch_array() — Выбирает одну строку из результирующего набора и помещает ее в ассоциативный массив, обычный массив или в оба
  • mysqli_stmt_store_result() — Передает результирующий набор запроса на клиента
Читайте также:  Javascript event which list

Источник

mysql_result

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide. Alternatives to this function include:

Description

Retrieves the contents of one cell from a MySQL result set.

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they’re MUCH quicker than mysql_result() . Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

Parameters

The result resource that is being evaluated. This result comes from a call to mysql_query() .

The row number from the result that’s being retrieved. Row numbers start at 0 .

The name or offset of the field being retrieved.

It can be the field’s offset, the field’s name, or the field’s table dot field name (tablename.fieldname). If the column name has been aliased (‘select foo as bar from. ‘), use the alias instead of the column name. If undefined, the first field is retrieved.

Return Values

The contents of one cell from a MySQL result set on success, or false on failure.

Examples

Example #1 mysql_result() example

$link = mysql_connect ( ‘localhost’ , ‘mysql_user’ , ‘mysql_password’ );
if (! $link ) die( ‘Could not connect: ‘ . mysql_error ());
>
if (! mysql_select_db ( ‘database_name’ )) die( ‘Could not select database: ‘ . mysql_error ());
>
$result = mysql_query ( ‘SELECT name FROM work.employee’ );
if (! $result ) die( ‘Could not query:’ . mysql_error ());
>
echo mysql_result ( $result , 2 ); // outputs third employee’s name

Читайте также:  Сайт

Notes

Note:

Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set.

See Also

  • mysql_fetch_row() — Get a result row as an enumerated array
  • mysql_fetch_array() — Fetch a result row as an associative array, a numeric array, or both
  • mysql_fetch_assoc() — Fetch a result row as an associative array
  • mysql_fetch_object() — Fetch a result row as an object

Источник

PHP get_result() and mysqli_stmt_get_result()

This article is created to cover the two functions of PHP, namely:

Both functions are used to get a result set from a prepared statement as a mysqli_result object. The only difference is that get_result() uses PHP mysqli object-oriented script, whereas mysqli_stmt_get_result() uses PHP mysqli procedural script.

PHP get_result()

The PHP get_result() function returns a result set from a prepared statement as a mysqli_result object in object-oriented style. For example:

connect_errno) < echo "Database connection failed!"; echo "Reason: ", $conn->connect_error; > else < $sql = "SELECT name, age FROM customer"; $stmt = $conn->prepare($sql); if($stmt->execute() == true) < $result = $stmt->get_result(); while($row = $result->fetch_array()) < echo "Name: ", $row[0]; echo ""; echo "Age: ", $row[1]; echo ""; > > > $conn->close(); ?>

The output of the above PHP example using the get_result() function is shown in the snapshot given below:

php get_result function

Note: The mysqli() function is used to open a connection to the MySQL database server in object-oriented style.

Note: The new keyword is used to create a new object.

Note: The connect_errno is used to get or return the error code (if any) from the last connect call in object-oriented style.

Note: The connect_error is used to get the error description (if any) from the last connection in object-oriented style.

Note: The prepare() function is used to prepare an SQL statement before its execution on the MySQL database in object-oriented style, to avoid SQL injection.

Читайте также:  Road map java разработчика

Note: The execute() function is used to execute a prepared statement on the MySQL database in object-oriented style.

Note: The fetch_array() function is used when we need to fetch and get the result as an enumerated array, an associative array, or both in object-oriented style.

Note: The close() function is used to close an opened connection in object-oriented style.

The above example can also be written as:

connect_errno) < $stmt = $conn->prepare("SELECT name, age FROM customer"); $stmt->execute(); $result = $stmt->get_result(); while($row = $result->fetch_array()) < echo "Name: ", $row[0]; echo ""; echo "Age: ", $row[1]; echo ""; > > $conn->close(); ?>

PHP get_result() Syntax

The syntax of the get_result() function in PHP is:

PHP mysqli_stmt_get_result()

The PHP mysqli_stmt_get_result() function returns a result set from a prepared statement as a mysqli_result object in procedural style. For example:

"; echo "Age: ", $row[1]; echo ""; > > mysqli_close($conn); ?>

Note: The mysqli_connect() function is used to open a connection to the MySQL database server in procedural style.

Note: The mysqli_connect_errno() function is used to get or return the error code (if any) from the last connect call in procedural style.

Note: The mysqli_prepare() function is used to prepare an SQL statement before its execution on the MySQL database in procedural style, to avoid SQL injection.

Note: The mysqli_stmt_execute() function is used to execute a prepared statement on the MySQL database in procedural style.

Note: The mysqli_fetch_array() function is used when we need to fetch and get the result as an enumerated array, an associative array, or both in procedural style.

Note: The mysqli_close() function is used to close an opened connection to the MySQL database in procedural style.

PHP mysqli_stmt_get_result() Syntax

The syntax of the mysqli_stmt_get_result() function in PHP is:

mysqli_stmt_get_result($mysqli_stmt)

Источник

Оцените статью