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

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序