Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySQL从表中读取以在图形中使用_Php_Mysql - Fatal编程技术网

PHP MySQL从表中读取以在图形中使用

PHP MySQL从表中读取以在图形中使用,php,mysql,Php,Mysql,我有一个基于几个值自动填充的图表。这些值是当前(当前位置)的目标(总计)和图形的总高度。我正试图将目标和当前数据从数据库中提取出来,并将其转换为货币格式,它应该填充图形到需要的位置 我让它处理一个URL get,它是?current=584559096&goal=100000000,我只使用了$\u get。为了便于维护周围的其他人,我想把它从一个数据库,可以更新后 到目前为止,我掌握的代码是: <? php function formatMoney($number, $fract

我有一个基于几个值自动填充的图表。这些值是当前(当前位置)的目标(总计)和图形的总高度。我正试图将目标和当前数据从数据库中提取出来,并将其转换为货币格式,它应该填充图形到需要的位置

我让它处理一个URL get,它是
?current=584559096&goal=100000000
,我只使用了$\u get。为了便于维护周围的其他人,我想把它从一个数据库,可以更新后

到目前为止,我掌握的代码是:

    <? php function formatMoney($number, $fractional=false) {
        if ($fractional) {
            $number = sprintf('%.0f', $number);
        }
        while (true) {
            $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
            if ($replaced != $number) {
                $number = $replaced;
            } else {
                break;
            }
        }
        return $number;
    }

        $goal = $row['goal'];
        $current = $row['current'];
        $percent = round($current / $goal * 100);
        $totalheight = 216;
        $ourheight = $totalheight * $percent / 100;

        $halfofarrowheight = 12;
            $arrowheight = $totalheight-$ourheight-$halfofarrowheight; 
            if($arrowheight < 0)
                $arrowheight = 0;
    mysql_close($con);
?>
这是我的错误警告:在第36行的E:\inetpub\wwwroot\test.website.net\web\includes\globalfoot.php中被零除

第36行是
$percent=round($current/$goal*100)

所以,我一直在愚弄这件事4天,现在只是想把目标列和当前列中的数字格式化为货币,并让图表填充。为了测试起见,让我们假设目标是10000000000,当前是5000000000

$row['goal'] = $goal;
$row['current'] = $current;
如果我没有理解您的代码,请原谅我,但上述内容不应该是:

$goal=$row['goal'];
$current=$row['current'];

总是,总是在除法时仔细检查变量

$percent = ($goal > 0 || $goal < 0) ? round($current / $goal * 100) : 0;
$percent=($goal>0 | |$goal<0)?回合($current/$goal*100):0;

您的代码应该如下所示:

mysql_select_db("databasename", $con);
$result = mysql_query("select * from `dbname`.`tablename");
if (!$result || !($row = mysql_fetch_assoc($result))) {
    echo "DB error: ".mysql_error();
    exit;
}

$goal = $row['goal'];
$current = $row['current'];
// ...

是的,我把它们调换了一下,看看它是否有用,但忘了调回。错误没有改变。再说一次,我不确定我是不是在教我奶奶吃蛋,但你在计算之前是否尝试过重复这两个变量的值?一个除法为零的警告提示$goal为零或空,你应该验证它是否被设置是的,我做了,他们的值出现了一个while($row=mysql\u fetch\u array($result))echo$row['goal']和$row['current'])。但是,仅仅做echo$row['goal']怎么样?@dmitry我不明白,在其余部分中,它只是用作Where is
$row
设置?你能给我看一下代码吗?谢谢你,马特,你把错误处理掉了。由于某种原因,尽管我的图表无法正常工作。甚至不回显箭头上的行和列。这就好像它甚至不读取db,即使测试回波显示它将拉出数字。我需要关闭页面底部的连接吗?希望我能检查两个答案,因为你们都提供了帮助。所以,也许你们的查询有问题?看到更新的代码@Dmitry是的,它起作用了,我错过了一个;这对PHP来说总是一件痛苦的事,错过一件小事就会毁了你的一天。希望我能检查两个答案,因为你们都帮助了我。非常感谢你们的帮助!我喜欢东西真正起作用的感觉。
mysql_select_db("databasename", $con);
$result = mysql_query("select * from `dbname`.`tablename");
if (!$result || !($row = mysql_fetch_assoc($result))) {
    echo "DB error: ".mysql_error();
    exit;
}

$goal = $row['goal'];
$current = $row['current'];
// ...