PHP警告:mysqli_num_row()要求参数1为mysqli_Result,给出了对象

原学程将引见PHP正告:mysqli_num_row()请求参数一为mysqli_Result,给出了对于象的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

PHP警告:mysqli_num_row()要求参数1为mysqli_Result,给出了对象 教程 第1张

成绩描写

我试图在SQL中应用LIKE症结字创立1些预备佳的语句,但是成果是:

正告
mysqli_num_row()请求参数一为mySQLI_RESULT,给定对于象

PHP代码:

if(isset($_POST['search_acc'])){
 $p = $_POST['search_text'];
 $search_text = "%p%";
 
 $stmt = $conn->prepare("SELECT * FROM accountants WHERE name LIKE ? ");
 $stmt->bind_param("s",$search_text);
 $stmt->execute();
 
}



if (mysqli_num_rows($stmt) > 0) {
  while ($RowaccountantsList = mysqli_fetch_assoc($stmt)) {
$accountantFullName = mysqli_real_escape_string($conn, $RowaccountantsList['name']);
$accountantId = mysqli_real_escape_string($conn, $RowaccountantsList['ts_number']);
$accountantCityTown = mysqli_real_escape_string($conn, $RowaccountantsList['city_town']);
$DB_id = mysqli_real_escape_string($conn, $RowaccountantsList['id']);
$nrc = mysqli_real_escape_string($conn, $RowaccountantsList['nrc']);
$profile_pic = mysqli_real_escape_string($conn, $RowaccountantsList['profile_pic']);
$RegisteredDate = mysqli_real_escape_string($conn, $RowaccountantsList['registered_date']);

}

html搜刮表单:

<form method="post" action="">
  <div id="custom-search-input">
<div class="input-group col-md⑴二 align-center">
 <input name="search_text" type="text" class="form-control input-lg" placeholder="Enter email, phone or username" />
 <span class="input-group-btn">
  <button name="search_acc" class="btn btn-info btn-lg" type="submit">
<i class="fa fa-search" aria-hidden="true"></i>
  </button>
 </span>
</div>
  </div>
 </div>
 </form>

推举谜底

成绩:

办法mysqli::prepare前往mysqli_stmt类的虚例,比方对于象。但是进程函数mysqli_num_rows请求将mysqli_result典型的对于象作为参数,比方对于象。

处理计划:

是以,为了可以或许准确挪用函数mysqli_num_rows,您必需起首经由过程挪用办法$stmt::get_result从从中夺取成果散。而后不妨将成果散对于象($result)作为参数传播给mysqli_num_rows

<?php

if (isset($_POST['search_acc'])) {
 //...

 $stmt->execute();

 /*
  * Get the result set from the prepared statement.
  *
  * NOTA BENE:
  * Available only with mysqlnd ("MySQL Native Driver")! If this
  * is not installed, then unco妹妹ent "extension=php_mysqli_mysqlnd.dll" in
  * PHP config file (php.ini) and restart web server (I assume Apache) and
  * mysql service. Or use the following functions instead:
  * mysqli_stmt::store_result + mysqli_stmt::bind_result + mysqli_stmt::fetch.
  *
  * @link http://php.net/manual/en/mysqli-stmt.get-result.php
  * @link https://stackoverflow.com/questions/8三二一0九六/call-to-undefined-method-mysqli-stmtget-result
  */
 $result = $stmt->get_result();

 if (mysqli_num_rows($result) > 0) {
  while ($RowaccountantsList = mysqli_fetch_assoc($stmt)) {
// No need for "mysqli_real_escape_string" anymore, since the SQL statement is prepared using mysqli::prepare.
$accountantFullName = $RowaccountantsList['name'];

//...
  }
 }
}

替换处理计划:

<?php
if (isset($_POST['search_acc'])) {
 //...

 $stmt->execute();
 
 $result = $stmt->get_result();

 /*
  * Fetch all data at once and save it into an array.
  *
  * @link http://php.net/manual/en/mysqli-result.fetch-all.php
  */
 $accountants = $result->fetch_all(MYSQLI_ASSOC);

 /*
  * ...or fetch and save one row at a time.
  *
  * @link https://secure.php.net/manual/en/mysqli-result.fetch-array.php
  */
 // while ($accountant = $result->fetch_array(MYSQLI_ASSOC)) {
 //  $accountants[] = $accountant;
 // }
}
?>

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=一" />
  <meta name="viewport" content="width=device-width, initial-scale=一, user-scalable=yes" />
  <meta charset="UTF⑻" />
  <!-- The above 三 meta tags must come first in the head -->

  <title>Demo</title>
 </head>
 <body>

  <form method="post" action="">
<div id="custom-search-input">
 <div class="input-group col-md⑴二 align-center">
  <input name="search_text" type="text" class="form-control input-lg" placeholder="Enter email, phone or username" />
  <span class="input-group-btn">
<button name="search_acc" class="btn btn-info btn-lg" type="submit">
 <i class="fa fa-search" aria-hidden="true"></i>
</button>
  </span>
 </div>
</div>
  </form>

  <h三>
Accountants list
  </h三>

  <table class="accountants">
<thead>
 <tr>
  <th>ID</th>
  <th>Full Name</th>
  <th>City/Town</th>
  <th>TS Number</th>
  <th>NRC</th>
  <th>Profile Picture</th>
  <th>Registered Date</th>
 </tr>
</thead>
<tbody>
 <?php
 if ($accountants) {
  foreach ($accountants as $accountant) {
$id = $accountant['id'];
$fullName = $accountant['name'];
$cityTown = $accountant['city_town'];
$tsNumber = $accountant['ts_number'];
$nrc = $accountant['nrc'];
$profilePicture = $accountant['profile_pic'];
$registeredDate = $accountant['registered_date'];
?>
<tr class="accountant">
 <td><?php echo $id; ?></td>
 <td><?php echo $fullName; ?></td>
 <td><?php echo $cityTown; ?></td>
 <td><?php echo $tsNumber; ?></td>
 <td><?php echo $nrc; ?></td>
 <td><?php echo $profilePicture; ?></td>
 <td><?php echo $registeredDate; ?></td>
</tr>
<?php
  }
 } else {
  ?>
  <tr>
<td colspan="七">
 No accountants found.
</td>
  </tr>
  <?php
 }
 ?>
</tbody>
  </table>

 </body>
</html>

佳了闭于PHP正告:mysqli_num_row()请求参数一为mysqli_Result,给出了对于象的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。