我需要有关如何正确执行此操作的帮助。我需要执行这个命令:
SELECT concat(branchname, -->, itemtype, '(, quantity, ')') from monitoring
order by itemtype;
语法在MySQL控制台中有效。但是,我在php上实现它有麻烦。我总是得到 “未定义的索引:branchname” “未定义的索引:itemtype” “未定义的索引:数量”
使用此代码:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
错误说它在这一行
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
我很困惑,因为我基本上运行了相同的代码,可以让我看到表格中的itemtype:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dex_test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT itemtype FROM monitoring";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "itemtype: " . $row["itemtype"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
帮助任何人?
答案 0 :(得分:0)
只需定义连接列的别名即可。使用此 -
SELECT concat(branchname,itemtype,quantity) as branchname from monitoring order by itemtype
或者如果你想分开那么 -
SELECT branchname, itemtype, quantityfrom monitoring order by itemtype
答案 1 :(得分:0)
您的查询似乎需要更新
"SELECT concat(branchname,itemtype,quantity) from monitoring order by itemtype";
应该是
"SELECT branchname,itemtype,quantity from monitoring order by itemtype";
我已经发布了这个答案,参考了你如何在while循环中调用你的字段
echo " " . $row["branchname"]. " " . $row["itemtype"]. " ".$row["quantity"]. "<br>";
如果你需要在一个字段中显示concat值而不是像
那样$sql = "SELECT concat(branchname,' ',itemtype,' ',quantity) as branch from monitoring order by itemtype";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["branch"]."<br>";
}
} else {
echo "0 results";
}