php - 如何将 mysqli_query 结果转换为 int?

标签 php mysql database mysqli

目前我正在开发一个移动图书馆应用程序,它需要借书。我希望能够检查与可用总数相比有多少本书被 checkout 。我正在努力将 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";
}
?>

感谢任何帮助,谢谢!

最佳答案

这会起作用

$result = $result->fetch_array();
$quantity = intval($result[0]);

关于php - 如何将 mysqli_query 结果转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48698663/

相关文章:

javascript - 从查询sql数据库获取数据到javascript

php - Doctrine findBy where and order

php - 使用 Angular.js 的 HTTP POST

MySQL left join 没有给我我所期望的

database - 事实表和维度表的区别?

php - 使用php获取两个位置之间的字符串部分

php - 将变量传递给外部 PHP 文件

MySQL 存储过程 vs 函数,我什么时候使用哪个?

asp.net-core - 连接 Crystal Report 和 DataSet

regex - Elasticsearch查询名称与特定模式匹配的索引