SQL查询工作在phpmyadmin中,但不在PHP脚本中

时间:2018-09-04 13:34:29

标签: php mysql sql xampp

无论何时我在phpmyadmin中尝试以下查询,都可以正常工作:

SELECT CAST(DATETIME AS DATE), Provider, COUNT(Provider) 
FROM purchases 
GROUP BY CAST(DATETIME AS DATE), Provider 
ORDER BY CAST(DATETIME AS DATE) DESC

尽管如此,当我将其放入PHP脚本时,我总是收到一个布尔值:

$sql="SELECT CAST(DATETIME AS DATE), Provider, COUNT(Provider) 
FROM purchases 
GROUP BY CAST(DATETIME AS DATE), Provider 
ORDER BY CAST(DATETIME AS DATE) DESC";


if (mysqli_query($con,$sql))
  {

    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) {
    echo $row['CAST(DATETIME AS DATE)'];
    echo $row['Provider'];
    echo $row['COUNT(Provider)'];

}

}
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }

mysqli_close($con)

;

我非常感谢有关如何解决此问题的一些建议。

1 个答案:

答案 0 :(得分:0)

此代码如下所示,因此可以正常工作

您在所有查询中都使用mysqli,并检查$result是否比打印记录有价值,否则打印了error消息

$result = mysqli_query($con,$sql);
if($result)
{
    while($row = mysqli_fetch_assoc($result)){
        echo $row['CAST(DATETIME AS DATE)'];
        echo $row['Provider'];
        echo $row['COUNT(Provider)'];

    }
}
else
{
    echo "Error creating table: " . mysqli_error($con);
}

mysqli_close($con)