php mysql从数据库中获取变量数据并计算
你好,我有个问题,我想从数据库中获取变量数据“$b,$d,$f,$h”,然后计算它 这里是我的例子php mysql从数据库中获取变量数据并计算,php,mysql,Php,Mysql,你好,我有个问题,我想从数据库中获取变量数据“$b,$d,$f,$h”,然后计算它 这里是我的例子 <?php $host="localhost"; $username="root"; $password="root"; $db_name="cbrteh" mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$d
<?php
$host="localhost";
$username="root";
$password="root";
$db_name="cbrteh"
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$b= mysql_query("SELECT bobot FROM atribut where id= 1");
($row = mysql_fetch_assoc($b));
$row["bobot"];
$d= mysql_query("SELECT bobot FROM atribut where id= 2");
($row = mysql_fetch_assoc($d));
$row["bobot"];
$f= mysql_query("SELECT bobot FROM atribut where id= 3");
($row = mysql_fetch_assoc($f));
$row["bobot"];
$h= mysql_query("SELECT bobot FROM atribut where id= 4");
($row = mysql_fetch_assoc($h));
$row["bobot"];
$calc = $b+$d+$f+$h;
echo $calc;
试试这个:
$b= mysql_query("SELECT bobot FROM atribut where id= 1");
$row = mysql_fetch_row($b);
$b = $row[0];
$d= mysql_query("SELECT bobot FROM atribut where id= 2");
$row = mysql_fetch_row($d);
$d = $row[0];
[...] // repeat for $f and $h
$calc = $b + $d + $f + $h;
echo($calc);
或者,可以使用任何更合适的代码:
$b= mysql_query("SELECT SUM(bobot) as calc FROM atribut WHERE id IN (1,2,3,4)");
$row = mysql_fetch_row($b);
$calc = $row[0];
echo($calc);
您应该将查询的值存储在变量中,然后求和
$b = $row["bobot"];
等等……您的代码中没有使用$row4
,不清楚您在问什么以及为什么要问它!为什么有这么多查询?从atribu中选择SUM(bobot),其中id在(1,2,3,4)中;这个问题的可能重复不可能是解决这个问题的最合适的方法——不管这个问题是什么!