duanliang9288 2013-10-07 10:44 采纳率: 100%
浏览 76
已采纳

使用bind_result&fetch()或store_result()代替get_result

How do I change this function to another function. I don't want to use the get_result

I searched online but could not find an answer that could help me.

public function Select($Table_Name, $Conditions='' ,$Array_Conditions_Limit=NULL , $OrderBy='', $Limit='', $Selected_Fields='*')
{
    $Query = "SELECT ".$Selected_Fields." FROM ".$Table_Name;
    if(!empty($Conditions))
        $Query .= " WHERE ".$Conditions;
    if(!empty($OrderBy))
        $Query .= " ORDER BY ".$OrderBy;
    if(!empty($Limit))
        $Query .= " LIMIT ".$Limit;

    $Statment = $this->ConnectionResult->prepare($Query);
    if(isset($Array_Conditions_Limit)  )
     {
        $Statment = $this->DynamicBindVariables($Statment, $Array_Conditions_Limit);
        $Statment->execute();
        return $Statment->get_result();
     }
     else
        $Statment->execute();
        return $Statment->get_result();
}

This also functions dynamic bind variables

private function DynamicBindVariables($Statment, $Params)
{
    if (is_array($Params) && $Params != NULL)
    {
        // Generate the Type String (eg: 'issisd')
        $Types = '';
        foreach($Params as $Param)
        {
            $Types .= $this->GetType($Param);
        }
        // Add the Type String as the first Parameter
        $Bind_names[] = $Types;

        // Loop thru the given Parameters
        for ($i=0; $i<count($Params);$i++)
        {
            $Bind_name = 'bind' . $i;
            // Add the Parameter to the variable 
            $$Bind_name = $Params[$i];
            // Associate the Variable as an Element in the Array
            $Bind_names[] = &$$Bind_name;
        }
        // Call the Function bind_param with dynamic Parameters
        call_user_func_array(array($Statment,'bind_param'), $Bind_names);
    }
    elseif(isset($Params) && !empty($Params))
    {
        $Types = '';
        $Types .= $this->GetType($Params);
        $Statment->bind_param($Types ,$Params);
    }
    return $Statment;
}

I using the return value as follows:

$myresult =Select('post','post_category=?' ,2  );
                $row = $myresul2->fetch_object()
  • 写回答

1条回答 默认 最新

  • dpbz14739 2013-10-07 11:55
    关注

    First of all, I find this approach utterly useless. What are you actually doing is dismembering fine SQL sentence into some anonymous parts.

    "SELECT * FROM post WHERE post_category=?"
    

    looks WAY better than your anonymous parameters of which noone have an idea.

    'post','post_category=?'
    

    One can tell at glance what does first statement to do. and have no idea on the second. Not to mention it's extreme:

    'post','post_category=?',NULL, NULL, 'username, password'
    

    So, instead of this kindergarten query builder I would rather suggest a function that accepts only two parameters - a query itself and array with bound data:

    $myresult = Select("SELECT * FROM post WHERE post_category=?", [2]);
    

    To make it more useful, I wouild make separate functions to get different result types, making your second line with fetch_object() obsolete (however, speaking of objects, they are totally useless to represent a table row). Example:

    $row = $db->selectRow("SELECT * FROM post WHERE post_category=?", [2]);
    

    Look: it's concise yet readable!

    As a further step you may wish to implement more placeholder types, to allow fields for ORDER BY clause be parameterized as well:

    $data = $db->getAll('id','SELECT * FROM t WHERE id IN (?a) ORDER BY ?n', [1,2],'f');
    

    you can see how it works, as well as other functions and use cases in my safeMysql library

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建