如何从数据库中检索值?

编程入门 行业动态 更新时间:2024-10-28 06:36:37
本文介绍了如何从数据库中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从数据库中获取变量数据 "$b ,$d, $f,$h" 然后计算它.这是我的例子:

I want to get variable data "$b ,$d, $f,$h" from database and then calculate it. Here is 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"); ($row4 = mysql_fetch_assoc($h)); $row["bobot"]; $calc = $b+$d+$f+$h; echo $calc; <?

数据库中的值是50、50、50、50,结果是22,这是为什么呢?

The values in the database are 50,50,50,50 but the result is 22. Why is this?

推荐答案

每个查询字符串下面的行是错误的

The line below every query string is wrong

($row = mysql_fetch_assoc($b)); $row["bobot"];

因为您没有将结果存储在任何地方.获取值的正确方法应该是:

Because you are not storing the result anywhere.The correct way to get the values should be:

if(count($res=mysql_fetch_assoc($b))>0)$_b=$res[0]['bobot'];

(如果返回结果至少有一行,返回值到$_b变量)

(if returning result has at least one row, return the value to $_b variable)

请注意 $b 用于存储查询结果,而不是您从中获得的值.

Mind that $b is being used to store the query result, not the value you get from it.

然后将结果相加如下:$calc = $_b+$_d+$_f+$_h; 就是这样.

Then you sum the results like this: $calc = $_b+$_d+$_f+$_h; and that's all.

更多推荐

如何从数据库中检索值?

本文发布于:2023-10-28 05:39:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!