Mysqli_result trong PHP

❮ PHP MySQLi Reference

Example - Object Oriented style

Perform query against a database:

$mysqli = new mysqli["localhost","my_user","my_password","my_db"];

// Check connection
if [$mysqli -> connect_errno] {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit[];
}

// Perform query
if [$result = $mysqli -> query["SELECT * FROM Persons"]] {
  echo "Returned rows are: " . $result -> num_rows;
  // Free result set
  $result -> free_result[];
}

$mysqli -> close[];
?>


Look at example of procedural style at the bottom.

Definition and Usage

The query[] / mysqli_query[] function performs a query against a database.

Syntax

Object oriented style:

$mysqli -> query[query, resultmode]

Procedural style:

mysqli_query[connection, query, resultmode]

Parameter Values

ParameterDescriptionconnectionRequired. Specifies the MySQL connection to usequeryRequired. Specifies the SQL query stringresultmode

Optional. A constant. Can be one of the following:

  • MYSQLI_USE_RESULT [Use this to retrieve large amount of data]
  • MYSQLI_STORE_RESULT [This is default]

Technical Details

Return Value:For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failurePHP Version:5+PHP Changelog:PHP 5.3.0 added the ability for async queries

Example - Procedural style

Perform query against a database:

$con = mysqli_connect["localhost","my_user","my_password","my_db"];

if [mysqli_connect_errno[]] {
  echo "Failed to connect to MySQL: " . mysqli_connect_error[];
  exit[];
}

// Perform query
if [$result = mysqli_query[$con, "SELECT * FROM Persons"]] {
  echo "Returned rows are: " . mysqli_num_rows[$result];
  // Free result set
  mysqli_free_result[$result];
}

mysqli_close[$con];
?>


❮ PHP MySQLi Reference

For those of you trying to bind rows into array,
$stmt = $db->prepare['SELECT id, name, mail, phone, FROM contacts'];
$stmt->execute[];
$stmt->store_result[];
$stmt->bind_result[$arr['id'], $arr['name'], $arr['mail'], $arr['phone']];
while [$stmt->fetch[]] {
    $outArr[] = $arr;
}
$stmt->close[];
return $outArr;
?>
this will give you all the rows you asked for except that they would all be the same as the first one because of some gremlins in the background code [i've heard that PHP is trying to save memory here].

But this one works:
$stmt = $db->prepare['SELECT id, name, mail, phone, FROM contacts'];
$stmt->execute[];
$stmt->store_result[];
$stmt->bind_result[$a,$b,$c,$d];
while [$stmt->fetch[]] {
    $outArr[] = ['id' => $a, 'name' => $b, 'mail' => $c, 'phone' => $d];
}
$stmt->close[];
return $outArr;
?>
Just don't use arrays to bind results :]

Please OH PLEASE.
I have been trying to get a result set from this function, and I had 0 luck completely, for nearly 3 hours!

If you ARE using mysqli_stmt_get_results[] to get a result set, in conjuction with mysqli_stmt_store_results in order to retrieve the number of rows returned, you are going to have some major trouble!

PHP Documentation states that to retrieve the number of rows returned by a prepared select sql statement, one should call the following statements respectively:

mysqli_stmt_execute[$stmt];
mysqli_stmt_store_result[$stmt];
$num_rows = mysqli_stmt_num_rows[$stmt];

THIS IS A MAJOR DEATH TRAP, IF YOU ARE USING mysqli_stmt_get_result[] in conjunction!!!! Results of doing so vary depending which statements you call first, but in the end, you will NOT get the desired result.

In conclusion, please, PLEASE, NEVER use mysqli_stmt_store_result[], then mysqli_ AND mysqli_stmt_get_result[] at the the same time. This is a MAJOR death trap.

SOLUTION:
If you are trying to get a result set, and you need the number of rows returned at the same time, use the following statements respectively instead:

$result_set = mysqli_stmt_get_results[$stmt];
$num_rows = mysqli_num_rows[$result_set];

Reflecting on my actions, this solution may seem fairly obvious. However, to someone new using PHP [like me] or someone who is not fully comfortable with prepared statements, it's very easy to get lost by using Google and learn on your own.

Summary:
NEVER use mysqli_stmt_store_result[$stmt] & mysqli_stmt_num_rows[$stmt] in conjunction with mysqli_stmt_get_result[$stmt]. You will regret it!! I have been stuck on this for hours, and Google offered me no answer!

Chủ Đề