dounou9751 2016-12-23 08:51
浏览 59

将mysql查询的结果对象转换为函数的问题

I have a function to process info from a database. This is called multiple times in a page. And I don't want to query the database every time. So I put the query outside. If I do that, the function doesn't work. I know this can be done because, there was a similar question somewhere in SO. But that addressed a different situation. I don't know what is wrong here. Any help will be greatly appreciated.

If I put all this code into a separate test file including the conn file and query, it works. But in my main page, where I have the functions.php included first, then conn.php and then the query and then the display code called by js fadein event, the $result refuses to work inside the function

EDIT : This code has been cleaned up as per comments received (globals replaced with variables passed to the function and variable names rationalised)

function total($item,$result,$val){
    global $totRate;
    while($getRates=$result->fetch_assoc()){    
    $gotItem= strtolower(preg_replace('/[^(\x20-\x7F)]*/',"",$getRates['item']));
    $gotItem=str_replace(array("_"," ","/"),"",$gotItem);

    if($item==$gotItem){
          $rate= $getRates['rate'];
          $totRate=$val*$rate;
        return $totRate;
        }
    }
}

The Result Call PHP file

$query = "SELECT * FROM rates ORDER BY item";
$result = $orderdb->query($query)

if (isset($_POST[$itemname]) && !empty($_POST[$itemname])) {
    $val=$_POST[$itemname];
    total($itemname);
    echo $totprate;
} else {
    echo "0";
}
  • 写回答

1条回答 默认 最新

  • drmy1050 2016-12-23 09:29
    关注

    I am writing this with the assumption that your SQL is working but are having problems displaying what you want - this may help. The code below saves your $result variable from your query and then passes it into the total function as a second parameter. Previously you were returning $totprate from total but you were not saving it anywhere - it is now saved to the $totprate variable.

    Note: I cannot see $orderdb anywhere in your code, I'm assuming you have that in your file and that it is working.

    function total($item, $result){
        global $val;
        global $pid;
        global $pitem;
        global $prate;
        global $totprate;
        global $gotitem;
        global $getratess;
        // global variable for $result removed so it doesn't overwrite variable passed to function
    
        while($getratess=$result->fetch_assoc()){
            $gotitem= strtolower(preg_replace('/[^(\x20-\x7F)]*/',"",$getratess['item']));
            $gotitem=str_replace(array("_"," ","/"),"",$gotitem);
    
            if ($item==$gotitem) {
                $pid=$getratess['id'];
                $pitem= $getratess['item'];
                $prate= $getratess['rate'];
                $totprate=$val*$prate;
                return $totprate;
            }
        }
    }
    
    $query = "SELECT * FROM rates ORDER BY item";
    $result = $orderdb->query($query);
    
    if (isset($_POST[$itemname]) && !empty($_POST[$itemname])) {
        $val=$_POST[$itemname];
        $totprate = total($val, $result); // pass itemname as first parameter and result array as second parameter and save it to the $totprate variable
        echo $totprate;
    } else {
        echo "0";
    }
    

    Let me know if this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图