目前,我正在开发一个移动图书馆应用,需要查看图书。我希望能够检查与可用总数相比检出的书籍数量。我正在努力将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";
}
?>
感谢任何帮助,谢谢!
答案 0 :(得分:1)
这将有效
$result = $result->fetch_array();
$quantity = intval($result[0]);
答案 1 :(得分:0)
你可以这样做:
$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";
}
?>
答案 2 :(得分:0)
这应该有效。请注意SQL注入。 此外,我对数据库中的库存书进行了疯狂猜测,需要进行更改。
require "conn.php";
$userID = "2";
$book_id = "1";
$mysql_qry = "SELECT
(SELECT COUNT(*) FROM books_checked_out WHERE bookID='" . $book_id . "') as num1,
(SELECT COUNT(*) FROM books_in_stock WHERE bookID='" . $book_id . "') as num2";
$result = mysqli_query($conn, $mysql_qry);
$quantity = mysqli_fetch_assoc($result);
if($quantity['num1'] >= 2) {
echo "more books checked out than in stock";
}
else {
echo "less books checked out than in stock";
}
//If you want to see the difference you can do something like this.
echo "Checked Out" . $quantity['num1'] . '<br/>';
echo "In Stock" . $quantity['num2'] . '<br/>';
答案 3 :(得分:0)
尝试使用
intval("0".$row['quantity'])
这会将您的数量转换为整数。