当MSQL查询的结果为空时,我在SELECT FOUND_ROWS()
返回0
时遇到问题。
我有以下调用getBasket();
function viewBasket(){
include('classes/Orders.php');
$BasketID = 10;
$numRows=100;
$data = Orders::getBasket( $numRows, $BasketID);
$results['basket'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
require( "templates/Basket.php" );
};
getBasket()
使用LEFT OUTER JOIN
构建所需的结果,并放入一个数组,然后返回viewBasket()
。
public static function getBasket( $numRows, $BasketID ) {
$order="Name ASC";
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "
SELECT SQL_CALC_FOUND_ROWS B.BasketID
, BP.ProductID
, BP.Quantity
, P.Name
, P.Price
, PT.NameType
FROM Basket B
LEFT
JOIN BasketProducts BP
ON B.BasketID = BP.BasketID
LEFT
JOIN Products P
ON BP.ProductID = P.ProductID
LEFT
JOIN ProductTypes PT
ON P.ProductTypeID = PT.ProductTypeID
WHERE B.BasketID = :BasketID
ORDER
BY $order
LIMIT :numRows;
";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->bindValue( ":BasketID", $BasketID, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$basket = new Orders( $row );
$list[] = $basket;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
我的HTML然后通过totalRows
$results
中存储的viewBasket();
值
<p>You have <?php echo $results['totalRows']?> item<?php echo ( $results['totalRows'] != 1 ) ? 's' : '' ?> in your Basket</p>
它有效,但如果表是空的,就不会发送0! 谢谢,亚当
答案 0 :(得分:1)
我无法在windows下使用php-5.6.3和mysql-5.6(默认:myisam)重现问题。
<?php
define('DB_DSN', 'mysql:host=localhost;dbname=test;charset=utf8');
define('DB_USERNAME', 'localonly');
define('DB_PASSWORD', 'localonly');
var_dump( getBasket(10, 1) );
function getBasket( $numRows, $BasketID ) {
$order="Name ASC";
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($conn); // boilerplate: creating empty, temp tables
$sql = "
SELECT
SQL_CALC_FOUND_ROWS
Basket.BasketID, BasketProducts.ProductID, BasketProducts.Quantity,
Products.Name, Products.Price, ProductTypes.NameType
FROM
soBasket as Basket
LEFT OUTER JOIN
soBasketProducts as BasketProducts
ON
Basket.BasketID = BasketProducts.BasketID
LEFT OUTER JOIN
soProducts as Products
ON
BasketProducts.ProductID = Products.ProductID
LEFT OUTER JOIN
soProductTypes as ProductTypes
ON
Products.ProductTypeID = ProductTypes.ProductTypeID
WHERE
Basket.BasketID = :BasketID
ORDER BY
" . $order . " LIMIT :numRows
";
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->bindValue( ":BasketID", $BasketID, PDO::PARAM_INT );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) {
$basket = new Orders( $row );
$list[] = $basket;
}
// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
class Orders {
public $_data;
public function __Construct(array $data) {
$this->_data = $data;
}
}
function setup($pdo) {
$queries = array(
"
CREATE TEMPORARY TABLE soBasket (
BasketID int
)
",
"
CREATE TEMPORARY TABLE soBasketProducts (
ProductID int,
BasketID int,
Quantity int
)
",
"
CREATE TEMPORARY TABLE soProducts (
ProductID int,
ProductTypeID int,
Price DECIMAL(10,2),
Name varchar(64)
)
",
"
CREATE TEMPORARY TABLE soProductTypes (
ProductTypeID int ,
NameType varchar(64)
)
"
);
foreach( $queries as $q ) {
$pdo->exec($q);
}
}
打印
array(2) {
'results' =>
array(0) {
}
'totalRows' =>
int(0)
}
答案 1 :(得分:0)
if($results['totalRows'] > 0){
echo $results['TotalRows'];
else{
echo '0';
}
IF
之前的echo
条件就可以了。
答案 2 :(得分:0)
尝试施展它:
return ( array ( "results" => $list, "totalRows" => (int)$totalRows[0] ) );
答案 3 :(得分:0)
感谢@VolkerK和@ Phate01的输入,我已经解决了这个问题。
如果从BasketID
返回的basket
为row
,我清除了basketProducts
表中的NULL
集。即如果我从与该用户关联的产品中删除了最后一个row
,请从Basket
移除购物车会话。
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$st = $conn->prepare ( "select BasketID FROM basketProducts WHERE BasketID = :basketID" );
$st->bindValue(":basketID", $basketID, PDO::PARAM_INT );
$st->execute();
$row = $st->fetch();
$conn = null;
/**
* If the row returned from BasketProducts DOES NOT include the customers $basketID
* remove the row.
**/
if($row == 0){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$st = $conn->prepare ( "DELETE FROM Basket WHERE BasketID = :basketID LIMIT 1" );
$st->bindValue(":basketID", $basketID, PDO::PARAM_INT );
$st->execute();
$row = $st->fetch();
$conn = null;
}else{
//do nothing
}
@VolkerK,我快速阅读了这个链接并且它已经过去了。今晚会读完,但你介意解释一下这对我有什么帮助吗?