Hướng dẫn php sql prepared statement get result - câu lệnh chuẩn bị php sql nhận kết quả

 $conn = new mysqli(.....);
 $param = $_GET['manf'];

 $stmt = $conn->prepare('select manf from manf where manf = ?');
 $stmt->bind_param('s', $param);
 $stmt->execute();
 $stmt->store_result();

 echo $stmt->num_rows;

 $result = $stmt->get_result();

 if(!$result){
     die(mysql_error());
 }
 while($row = $result->fetch_assoc()){
     echo $row['manf'];
 }

echo $stmt->num_rows in đúng VAULE Tuy nhiên tôi không thể nhận được kết quả trong khi câu lệnh. Tôi cũng đã thử mysqli::bind_result nhưng không hoạt động. Làm thế nào tôi có thể sửa lỗi này?
How can I fix this?

Hỏi ngày 25 tháng 10 năm 2015 lúc 13:01Oct 25, 2015 at 13:01

Hướng dẫn php sql prepared statement get result - câu lệnh chuẩn bị php sql nhận kết quả

3

Thử cái này:

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();

Để tìm nạp bạn cần sử dụng $stmt->fetch().

Marvin Klar

1.8212 Huy hiệu vàng13 Huy hiệu bạc31 Huy hiệu đồng2 gold badges13 silver badges31 bronze badges

Đã trả lời ngày 25 tháng 10 năm 2015 lúc 13:11Oct 25, 2015 at 13:11

Megh Vidanimegh VidaniMegh Vidani

6051 Huy hiệu vàng6 Huy hiệu bạc22 Huy hiệu đồng1 gold badge6 silver badges22 bronze badges

2

mysqli_stmt_get_result

"$ STMT" Rõ ràng (tôi nghĩ) là viết tắt của "tuyên bố". Là một tên biến là tùy ý, bạn có thể đặt tên cho biến đó bất cứ thứ gì bạn muốn. $ STMT chỉ là khá thành ngữ. Một tuyên bố đã chuẩn bị như vậy là một tính năng cơ sở dữ liệu.

(Php 5> = 5.3.0, Php 7, Php 8) -- mysqli_stmt_get_resultGets a result set from a prepared statement as a mysqli_result object

mysqli_stmt :: get_result - mysqli_stmt_get_result - nhận được kết quả được đặt từ một câu lệnh đã chuẩn bị như một đối tượng mysqli_result

Sự mô tả

Phong cách hướng đối tượng mysqli_stmt::get_result(): mysqli_result|false

Ghi chú::

Chỉ có sẵn với mysqlnd.

Ghi chú::

Hàm này không thể được sử dụng cùng với mysqli_stmt_store_result (). Cả hai chức năng này đều truy xuất kết quả đầy đủ được đặt từ máy chủ MySQL.mysqli_stmt_store_result(). Both of these functions retrieve the full result set from the MySQL server.

Trả về giá trị

Trả lại false về thất bại. Đối với các truy vấn thành công tạo ra một tập kết quả, chẳng hạn như SELECT, SHOW, DESCRIBE hoặc EXPLAIN, mysqli_stmt_get_result () sẽ trả về một đối tượng mysqli_result. Đối với các truy vấn thành công khác, mysqli_stmt_get_result () sẽ trả về false. Hàm mysqli_stmt_errno () có thể được sử dụng để phân biệt giữa hai lý do cho false; Do lỗi, trước Php 7.4.13, mysqli_errno () đã được sử dụng cho mục đích này.false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_stmt_get_result() will return a mysqli_result object. For other successful queries, mysqli_stmt_get_result() will return false. The mysqli_stmt_errno() function can be used to distinguish between the two reasons for false; due to a bug, prior to PHP 7.4.13, mysqli_errno() had to be used for this purpose.

Ví dụ

Ví dụ #1 Phong cách hướng đối tượng

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
1

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
2

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
3

Ví dụ #2 Phong cách thủ tục

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
1

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
2

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
6

Các ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

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

Xem thêm

  • mysqli_prepare () - Chuẩn bị một câu lệnh SQL để thực thi
  • MySQLI_STMT_RESULT_METADATA () - Trả về SETADATA SETADATA từ một câu lệnh đã chuẩn bị
  • mysqli_stmt_fetch () - Tìm nạp kết quả từ một câu lệnh đã chuẩn bị vào các biến bị ràng buộc
  • mysqli_fetch_array () - Lấy hàng tiếp theo của kết quả được đặt thành một kết hợp, một mảng số hoặc cả hai
  • mysqli_stmt_store_result () - lưu trữ kết quả được đặt trong bộ đệm bên trong

Ẩn danh ¶

9 năm trước

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
7

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
8

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();
9

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

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

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

Anon ¶

4 năm trước

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

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

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

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

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

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

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

echo $stmt->num_rows0

echo $stmt->num_rows1

echo $stmt->num_rows2

echo $stmt->num_rows3

Jeffspicer tại Gmail Dot Com ¶

4 năm trước

echo $stmt->num_rows4

echo $stmt->num_rows5

echo $stmt->num_rows6

echo $stmt->num_rows3

Jeffspicer tại Gmail Dot Com ¶

Jari Dot Wiklund tại Gmail Dot Com ¶

echo $stmt->num_rows8

11 năm trước

Haravikk ¶

echo $stmt->num_rows9

mysqli::bind_result0

mysqli::bind_result1

echo $stmt->num_rows3

6 năm trước

Headlessdev ¶

mysqli::bind_result3

mysqli::bind_result4

mysqli::bind_result5

mysqli::bind_result6

echo $stmt->num_rows3

Chuẩn bị () trong PHP là gì?

Một câu lệnh đã chuẩn bị là một tính năng được sử dụng để thực hiện các câu lệnh SQL (hoặc tương tự) giống nhau với hiệu quả cao.Các câu lệnh được chuẩn bị về cơ bản hoạt động như thế này: Chuẩn bị: Một mẫu câu lệnh SQL được tạo và gửi đến cơ sở dữ liệu.a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database.

Chúng ta có thể sử dụng câu lệnh đã chuẩn bị cho truy vấn chọn trong PHP không?

Bạn phải luôn luôn sử dụng các câu lệnh đã chuẩn bị cho bất kỳ truy vấn SQL nào có chứa biến PHP.Để làm như vậy, luôn luôn làm theo các bước dưới đây: Tạo câu lệnh SQL Chọn chính xác.. To do so, always follow the below steps: Create a correct SQL SELECT statement.

Mysqli là gì

MySQLI :: Chuẩn bị - MySQLI_Prepare - Chuẩn bị một câu lệnh SQL để thực thi.Prepares an SQL statement for execution.

$ STMT trong php mysqli là gì?

"$ STMT" Rõ ràng (tôi nghĩ) là viết tắt của "tuyên bố".Là một tên biến là tùy ý, bạn có thể đặt tên cho biến đó bất cứ thứ gì bạn muốn.$ STMT chỉ là khá thành ngữ.Một tuyên bố đã chuẩn bị như vậy là một tính năng cơ sở dữ liệu.