简体   繁体   中英

MySQL Query works perfectly in Workbench, returns no value from my php dashboard?

Have an odd one for you...

I have a dashboard which displays data from a BMS system, it's written in php, html & css with a splattering of java. I have a MySQL query which I can run in MySQL workbench and it returns the expected result. However when I run the same MySQLi query from my webpage no value is returned? The Table is called:- currentValues and the column with the BMS values in it is called:- currentValue, again out of my control [designed by BMS vendor] referencePath is the column with the BMS endpoints in. The database is selected as part of my $conn string...

Here is my MySQL Workbench query:

SELECT SUBSTRING(currentValue, 1, 3) FROM currentValues WHERE referencePath LIKE '%CO2_Level'

Here is my webpage query:

$sql = "SELECT SUBSTRING(currentValue, 1, 3) FROM currentValues WHERE referencePath LIKE '%CO2_Level'";
        $result = $conn->query($sql);
                $row = mysql_fetch_object($result);
                echo "<h1>" . $row . " PPM</h1>";

The reason I am using SUBSTRING is because the value has 4 decimal places and the column is a VARCHAR [which I can't change], I only want the whole number [well, technically it's a string].

I have also tried the alternative to SUBSTRING -> LEFT(currentValue, 3) but this behaves in the same way? Any suggestions would be greatly received!!!

EDIT...

However if I use the query without the SUBSTRING()

$sql = "SELECT * FROM currentValues WHERE referencePath LIKE '%CO2_Level'";
        $result = $conn->query($sql);
                $row = mysql_fetch_object($result);    
                echo "<h1>" . $row["currentValue"] . " PPM</h1>";

All works as expected except I have a silly value in my Dashboard like 627.1498 PPM

WORKAROUND

This question was closed as DUPLICATE to an unrelated question so I am unable to post this as the answer. Although really it's a workaround not the answer for the above, but may be helpful for others, I simply used the php operator ROUND to round the value returned in my MySql query.

echo "<h1>" . round($row["currentValue"]) . "PPM</h1>";

You are using SUBSTRING(currentValue, 1, 3) in select, So it will give you SUBSTRING(currentValue, 1, 3) in your result.

You can use it by $row->SUBSTRING(currentValue, 1, 3).

But instead, you should use an alias for it.

Try -

$sql = "SELECT SUBSTRING(currentValue, 1, 3) AS curr_value FROM currentValues WHERE referencePath LIKE '%CO2_Level'";
$result = $conn->query($sql);
$row = mysql_fetch_object($result);    
echo "<h1>" . $row->curr_value . " PPM</h1>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM