php mysql从数据库获取变量数据并计算 [英] php mysql get variable data from database and calculate
本文介绍了php mysql从数据库获取变量数据并计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我的溢出者有一个问题,我想从数据库中获取变量数据"$ b,$ d,$ f,$ h",然后计算出来 这是我的例子
hello fellow overflower i have problem iwant to get variable data "$b ,$d, $f,$h" from database and then calculate it here my example
<?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;
我的脚本正确吗 数据库上的值是50,50,50,50,所以值必须是200 但是我从echo $ calc得到的值;是22
is my script right the value on database are 50,50,50,50 so the value must be 200 but the value i got from echo $calc; is 22
推荐答案
尝试一下:
$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);
或者,也许使用任何更合适的代码:
Or, maybe used any more appropriate code :
$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);
这篇关于php mysql从数据库获取变量数据并计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文