gpt4 book ai didi

php - 创建 SQL 语句时出错

转载 作者:行者123 更新时间:2023-11-29 21:09:32 25 4
gpt4 key购买 nike

我编写了以下代码来尝试获取数据库中 3 个表中的条目数。当我在 phpMyAdmin 中使用查询时,这些查询可以工作,但是当我运行此代码时,我得到:

Error creating SQL statement

它是由 if(!$stmt) 语句生成的,我不知道为什么它不起作用。预先感谢:)

<?php
include 'connection.php';

$countArtists = $countAlbums = $countTracks = 0;

/* Create queries to get counts from each table */
$sql = "SELECT COUNT(*) FROM artist;";
$sql .= "SELECT COUNT(*) FROM cd;";
$sql .= "SELECT COUNT(*) FROM tracks;";

$stmt = $conn->prepare($sql);
if(!$stmt)
{
echo "Error creating SQL statement";
return 1;
}

$stmt->execute();

$stmt->bind_result($countArtists, $countAlbums, $countTracks);

echo "<li>Number of Artists: $countArtists</li><br>\n" .
"<li>Number of Albums: $countAlbums</li><br>\n" .
"<li>Number of Tracks: $countTracks</li><br>\n";
?>

最佳答案

我想你想要一个像这样的查询:

$sql = "SELECT (SELECT COUNT(*) FROM artist) as countArtists, ".
"(SELECT COUNT(*) FROM cd) as countAlbums, ".
"(SELECT COUNT(*) FROM tracks) as countTracks";

这是一个包含三列的查询,而不是三个单独的查询。

关于php - 创建 SQL 语句时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com