MySQL查询来自PHP的知道有一个值,但它是空的。

huangapple go评论55阅读模式
英文:

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 = &quot;SELECT * FROM HoldingUnit WHERE tankfacility = &#39;test&#39; AND net = &#39;0&#39; ORDER BY name&quot;; // Select Holding Units that are not nets
$resulttankonly = $conn-&gt;query($sqltankonly); // Select Holding Units that are not nets unless they have something in them


if ($resulttankonly-&gt;num_rows &gt; 0) {
  // Setup Table
  echo &#39;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Destination Tank&lt;/th&gt;&lt;th&gt;Net&lt;/th&gt;&lt;/tr&gt;&#39;;
  while($row = $resulttankonly-&gt;fetch_assoc()) {
    echo &#39;&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;radio&quot; name=&quot;dtank&quot; &quot; value=&quot;&#39; . $row[&quot;name&quot;] . &#39;&quot; required&gt;&#39; . $row[&quot;name&quot;] . &#39;&lt;/td&gt;&#39;;
    echo &#39;&lt;td&gt;&#39;;

// Below is my problem *******************

    $sqlnetcapacity = &#39;SELECT MAX(net) FROM HoldingUnit WHERE name=&quot;&#39; . $row[&quot;name&quot;] . &#39;&quot;&#39;;
    $resultnetcapacity = $conn-&gt;query($sqlnetcapacity);
    $netcapacity = $conn-&gt;query(&#39;SELECT MAX(net) FROM HoldingUnit WHERE name=&quot;Test-01&quot;&#39;)-&gt;fetch_object()-&gt;net;
    while($netcapacity &gt; 0) {
      echo &#39;&lt;input type=&quot;radio&quot; name=&quot;net&quot; value=&quot;&#39; . $netcapacity . &#39;&quot; /&gt;&#39; . $netcapacity . &#39;;
      $netcapacity = $netcapacity - 1;
    }
  }
  echo &#39;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&#39;;

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-&gt;query(&#39;SELECT MAX(net) AS net FROM ...&#39;)-&gt;fetch_object()-&gt;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[&#39;net_capacity&#39;]

答案2

得分: 0

感谢您帮助我找到正确的方向。以下查询给我提供了所需的结果...

SELECT m.name, m.net
FROM HoldingUnit m
    LEFT JOIN HoldingUnit b
        ON m.name = b.name
        AND m.net &lt; 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 &lt; 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.

huangapple
  • 本文由 发表于 2023年4月11日 04:55:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定