sql查询在phpmyadmin中工作,但在php脚本中不工作

v1uwarro  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(333)

这个问题在这里已经有答案了

我可以在php中混合mysql api吗(4个答案)
两年前关门了。
每当我在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)

;
我真的很想得到一些关于如何解决这个问题的建议。

ktca8awb

ktca8awb1#

此代码如下所示,所以它的工作很好
你用 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)

相关问题