目前我正在开发一个移动图书馆应用程序,它将需要检查书籍。我想能够检查出有多少书,相比之下,总的可用。我正在努力将mysqli_query
结果转换成一个整数,以与可用的图书总量进行比较。我很抱歉,如果这是一个简单的答案(我很新),因为我做了相当多的挖掘,无法找到一个答案,我可以适应我的情况。
<?php
require "conn.php";
$userID = "2";
$book_id = "1";
$mysql_qry = "select COUNT(*) from books_checked_out where bookID=$book_id";
$result = mysqli_query($conn, $mysql_qry);
//Somewhere here convert the $result into $quantity, which would be an int
if($quantity >= 2) {
echo "more books checked out than in stock";
}
else if($quantity < 2) {
echo "less books checked out than in stock";
}
?>
感谢您的帮助!
最佳答案
你可以这样做:
$userID = "2";
$book_id = "1";
$mysql_qry = "select COUNT(*) as quantity from books_checked_out where bookID=$book_id";
$result = mysqli_query($conn, $mysql_qry);
$row = mysqli_fetch_array($result, 'MYSQLI_ASSOC'); // Use something like this to get the result
$quantity = $row['quantity'];
if($quantity >= 2) {
echo "more books checked out than in stock";
}
else if($quantity < 2) {
echo "less books checked out than in stock";
}
?>