我试图从MySQL语句中获取行数,并创建一个if-else语句。但是,我尝试了几次,但似乎没有用。有人可以告诉我我在做什么错吗?还是有更好的方法来做到这一点

PHP的:

    $sqlquery_str = "SELECT * FROM Promotions WHERE membershipType = :membershipType AND promoStatus = '1' AND CONVERT(datetime, startDate, 103)  <= convert(varchar(8), GETDATE(), 112) AND CONVERT(datetime, endDate, 103)  >= convert(varchar(8), GETDATE(), 112) AND startTime <= CONVERT(char(5), GETDATE(),108) AND endTime >= CONVERT(char(5), GETDATE(),108)";

    $number_of_rows =  mysql_fetch_array($sqlquery_str);
    $total = $number_of_rows[0];


否则声明:

    if (!$results){
        logError('SQL Query failed',print_r($conns->errorInfo(),true));
        echo '
        <b>There was an error retrieving the information for the selected event. Please contact the administrator for further advice.</b>';
        }

    else if ($total == 1){
        echo "There is an error";
        echo "<br />";
        }

    else {
       $DOMstr = "<h1 class='post-title'>".$results['titlePromo']."</h1>";
        }

最佳答案

使用mysql_num_rows()

<?php
     $sqlquery_str = "SELECT * FROM Promotions WHERE membershipType = :membershipType AND promoStatus = '1' AND CONVERT(datetime, startDate, 103)  <= convert(varchar(8), GETDATE(), 112) AND CONVERT(datetime, endDate, 103)  >= convert(varchar(8), GETDATE(), 112) AND startTime <= CONVERT(char(5), GETDATE(),108) AND endTime >= CONVERT(char(5), GETDATE(),108)";

    $number_of_rows = mysql_num_rows($sqlquery_str);

    if ($number_of_rows > 0)
    {
        $results = mysql_fetch_array($sqlquery_str);
        $DOMstr = "<h1 class='post-title'>".$results['titlePromo']."</h1>";
    }
    else
    {
        echo "There is an error";
        echo "<br />";
    }
?>

07-24 15:03