英文:
MYSQL query from PHP knows there is a value but it's blank
问题
以下是代码部分的翻译:
$sqltankonly = "SELECT * FROM HoldingUnit WHERE tankfacility = 'test' AND net = '0' ORDER BY name"; // 选择不是网的持有单位
$resulttankonly = $conn->query($sqltankonly); // 选择不是网的持有单位,除非它们内部有数据
if ($resulttankonly->num_rows > 0) {
// 设置表格
echo '<table><tr><th>Destination Tank</th><th>Net</th></tr>';
while ($row = $resulttankonly->fetch_assoc()) {
echo '<tr><td><input type="radio" name="dtank" value="' . $row["name"] . '" required>' . $row["name"] . '</td>';
echo '<td>';
// 以下是我的问题 *******************
$sqlnetcapacity = 'SELECT MAX(net) FROM HoldingUnit WHERE name="' . $row["name"] . '"';
$resultnetcapacity = $conn->query($sqlnetcapacity);
$netcapacity = $conn->query('SELECT MAX(net) FROM HoldingUnit WHERE name="Test-01"')->fetch_object()->net;
while ($netcapacity > 0) {
echo '<input type="radio" name="net" value="' . $netcapacity . '" />' . $netcapacity;
$netcapacity = $netcapacity - 1;
}
}
echo '</td></tr></table>';
}
这是您提供的代码的中文翻译。如有任何其他疑问,请随时提出。
英文:
I have a query that looks for the highest value of a given name in a table and returns that number. It works great in the SQL console. But, I am trying to get that value and then create radio buttons in an HTML table and although there is data, it's an empty set. It will always be just one value, so an array is not needed. There can be multiple entries in the table with the same name, but not also with the same net.
$sqltankonly = "SELECT * FROM HoldingUnit WHERE tankfacility = 'test' AND net = '0' ORDER BY name"; // Select Holding Units that are not nets
$resulttankonly = $conn->query($sqltankonly); // Select Holding Units that are not nets unless they have something in them
if ($resulttankonly->num_rows > 0) {
// Setup Table
echo '<table><tr><th>Destination Tank</th><th>Net</th></tr>';
while($row = $resulttankonly->fetch_assoc()) {
echo '<tr><td><input type="radio" name="dtank" " value="' . $row["name"] . '" required>' . $row["name"] . '</td>';
echo '<td>';
// Below is my problem *******************
$sqlnetcapacity = 'SELECT MAX(net) FROM HoldingUnit WHERE name="' . $row["name"] . '"';
$resultnetcapacity = $conn->query($sqlnetcapacity);
$netcapacity = $conn->query('SELECT MAX(net) FROM HoldingUnit WHERE name="Test-01"')->fetch_object()->net;
while($netcapacity > 0) {
echo '<input type="radio" name="net" value="' . $netcapacity . '" />' . $netcapacity . ';
$netcapacity = $netcapacity - 1;
}
}
echo '</td></tr></table>';
I tried treating $resultnetcapacity like an array and used a while loop instead of fetch_object(). I have tried just printing the values and nothing shows even if an if statement proves there are more than 0 values.
答案1
得分: 2
There's no net
column in the result; the column you selected is named MAX(net)
. You should assign an alias to the value of MAX(net)
.
$netcapacity = $conn->query('SELECT MAX(net) AS net FROM ...')->fetch_object()->net;
There's also no need to do these queries inside the loop. You can join the original query with the MAX()
queries.
SELECT t1.*, t2.net_capacity
FROM HoldingUnit AS t1
JOIN (
SELECT name, MAX(net) AS net_capacity
FROM HoldingUnit
GROUP BY name
) AS t2 ON t1.name = t2.name
Then use $netcapacity = $row['net_capacity']
英文:
There's no net
column in the result; the column you selected is named MAX(net)
. You should assign an alias to the value of MAX(net)
.
$netcapacity = $conn->query('SELECT MAX(net) AS net FROM ...')->fetch_object()->net;
There's also no need to do these queries inside the loop. You can join the original query with the MAX()
queries.
SELECT t1.*, t2.net_capacity
FROM HoldingUnit AS t1
JOIN (
SELECT name, MAX(net) AS net_capacity
FROM HoldingUnit
GROUP BY name
) AS t2 ON t1.name = t2.name
Then use $netcapacity = $row['net_capacity']
答案2
得分: 0
感谢您帮助我找到正确的方向。以下查询给我提供了所需的结果...
SELECT m.name, m.net
FROM HoldingUnit m
LEFT JOIN HoldingUnit b
ON m.name = b.name
AND m.net < b.net
WHERE b.net IS NULL;
以下是我的结果...
+---------+------+
| name | net |
+---------+------+
| Test-02 | 0 |
| Test-03 | 0 |
| Test-04 | 0 |
| Dead | 0 |
| Test-01 | 1 |
+---------+------+
谢谢。随着我添加更多的储罐和更多的网络到这些储罐,这似乎满足了我的需求。
英文:
Thank you for the help to point me in the right direction. The below query gave me the result I needed...
SELECT m.name, m.net
FROM HoldingUnit m
LEFT JOIN HoldingUnit b
ON m.name = b.name
AND m.net < b.net
WHERE b.net IS NULL;
And here is my result...
+---------+------+
| name | net |
+---------+------+
| Test-02 | 0 |
| Test-03 | 0 |
| Test-04 | 0 |
| Dead | 0 |
| Test-01 | 1 |
+---------+------+
Thank you. As I add more tanks and more nets to those tanks, this seems to work for my requirements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论