2013-10-07 33 views
0

如何将此函数更改为另一个函数。我不想使用get_result使用bind_result&fetch()或store_result()代替get_result

我在网上搜索,但找不到可以帮助我的答案。

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(); 
} 

这里也可以动态绑定变量

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; 
} 
我使用的返回值如下

所有的
$myresult =Select('post','post_category=?' ,2 ); 
       $row = $myresul2->fetch_object() 
+1

我会回答,但这个无用的'select()'函数排斥我。我不知道为什么这个[不方便且完全无法使用]的方法对PHP用户非常有吸引力。 –

+0

你能告诉我更多关于它的信息吗?为什么没用? – majid

+0

由于出色的SQL语言几乎像自然英语一样读取,所以您制作了一个丑陋的科学怪人,没有人可以读取(颠倒读取),并且可以让您只运行愚蠢的查询,从而使其无法处理任何复杂的查询。所有这些都可以为你节省三个字的输入 –

回答

1

首先,我觉得这种做法完全无用。你究竟在做什么,是肢解罚款SQL语句成一些匿名的部分。

"SELECT * FROM post WHERE post_category=?" 

看起来比你没有任何想法的匿名参数更好。

'post','post_category=?' 

人们可以一目了然地看出第一个陈述要做什么。对第二个也不知道。且不说它的极端:

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

因此,而不是这个幼儿园查询生成器,我宁愿建议,只接受两个参数的函数 - 一个查询本身和阵列绑定数据:

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

要使它更有用,我开发了单独的函数来获得不同的结果类型,使得你的第二行fetch_object()已经过时(但是,对于对象来说,它们完全没有用来表示表行)。例如:

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

看起来:它简洁而易读!

作为进一步的步骤中,您可能希望实现更多的占位符类型,允许BY子句对ORDER领域进行参数以及:

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

你可以看到它是如何工作的,以及其他功能和使用方法在我的案件safeMysql library

+0

感谢我学到的有用的东西。可以给我一个例子来解决这个问题吗?替换get_result – majid

+0

http://www.php.net/manual/en/mysqli-stmt.bind-result.php在这里你会发现一对夫妇 –