似乎没有为MySqli Php"排序"

  • 本文关键字:Php 排序 MySqli php mysqli
  • 更新时间 :
  • 英文 :


我在PHP中的MySqli中的Order by遇到问题。要么是bind_param不起作用,要么是其他原因。我不知道我在这里忘了什么,它只是不想接受我的束缚。提前感谢。

@ $db = new mysqli('localhost', 'root', '', 'books');
// if mysqli_connect_errno() is set, we did not successfully connect. Here we deal with the error.
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.  Please try again later.</body></html>';
die();
}
$sortOrder = 'title';
$query = "SELECT ISBN, Author, Title, Price FROM books ORDER BY ?";
$stmt = $db->prepare($query);
$stmt->bind_param('s', $sortOrder);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($isbn, $author, $title, $price);
echo "<p>Number of books found: " . $stmt->num_rows . "</p>";
$counter = 0;
if ($stmt->num_rows > 0) {
while ($stmt->fetch()) {
$newBook = new book($isbn, $author, $title, $price);
$bookList[$counter] = $newBook;
$counter++;
}
} else {
//Nothing
}
$stmt->free_result();
$db->close();

不能绑定标识符(表/列(,只能绑定值。使用白名单来检查该值是已知对中的一个,如果是,则将其传入。

if(in_array($column, array('title', 'possible_other_column'))) {
$query = "SELECT ISBN, Author, Title, Price FROM books ORDER BY $column";
$stmt = $db->prepare($query);
$stmt->execute();
} else {
echo 'Column is not a valid name please select a valid column';
//or whatever behavior you want to happen, maybe just use a default column

最新更新