PDA

View Full Version : استفاده از قابلیت prepare در pdo و mysqli



majid1605
چهارشنبه 27 شهریور 1392, 00:34 صبح
public function Select_By_Order_Condi($Table_Name, $Conditions='', $OrderBy='', $Limit='', $Selected_Fields='*')
{
$sql = 'SELECT '.$Selected_Fields.' FROM '.$Table_Name;
if(!empty($Conditions))
$sql .= ' WHERE '.$Conditions;
if(!empty($OrderBy))
$sql .= ' ORDER BY '.$OrderBy;
if(!empty($Limit))
$sql .= ' LIMIT '.$Limit;
return $this->SqlQuery($sql);
}


سلام من تا حالا برای ایمن سازی داده ها از روشهای معمول مثل اسکیپ کردن استفاده می کردم ولی یکی از قابلیتهای pdo و mysqli ویژگی prepare میشه به عنوان مثال این تابع رو برای این دوتا به عنوان نمونه پیاده سازی کنید.
تووی mysqli بیشتر مشکل دارم نامگذاری متغیرها خیلی عجیبه توجه نمیشم.

اینم تابع query


private function SqlQuery($sql)
{

$this->ConnectionResult->query('SET NAMES UTF8;');
$return_result=$this->ConnectionResult->query($sql);

if ($return_result)
{
return $return_result;
}
else
{
$this->Error($sql);
}
}


فرض کنید شرطمون یه شرط چندتایی



$Conditions= id = $ID AND name =$name

majid1605
چهارشنبه 27 شهریور 1392, 12:09 عصر
اینو نوشتم ولی کار نمی کنه یعنی نمیدونم به تابع bind_param چطور آرایه رو ارسال کنم


public function SelectByOrderCondi($Table_Name, $Conditions='' ,$Array_Conditions_Limit=null, $OrderBy='', $Limit='', $Selected_Fields='*')
{

if(isset($Array_Conditions_Limit))
{
$S = "";
foreach($Array_Conditions_Limit as $Value)
{
$Valuse[] = $Value ;
if(is_string($Value))
$S.='s';
continue ;
if(is_int($Value))
$S.='i';
continue ;
if(is_double($Value))
$S.='d';
}
}
$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;

$query = $this->ConnectionResult->prepare($Query);
$query->bind_param($Valuse);
return $query->execute();
}

majid1605
چهارشنبه 27 شهریور 1392, 23:06 عصر
تقریبا موفق به نوشتنش با mysqli شدم ولی نمی دونم اصلا این قابلیت رو به درستی به کار گرفتم یا نه ؟
بعضی جاها خوب کار می کنه بعضی جاها ایرادهای مختلف نمیدونم کجای کار اشتباه ست.



public function SelectByOrderCondi($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
return false ;

}






// $Statment = The SQL Statement Object
// $Params = Array of the Parameters
public function DynamicBindVariables($Statment, $Params)
{
if (is_array($Params) && $Params != null)
{
// Generate the Type String (eg: 'issisd')
$Types = '';
foreach($Params as $Param)
{
if(is_int($Param)) //Int
$Types .= 'i';
elseif (is_float($Param)) // Double
$Types .= 'd';
elseif (is_string($Param)) // String
$Types .= 's';
else // Blob and Unknown
$Types .= 'b';
}
// Add the Type String as the first Parameter
$Bind_names[] = $Types;

// Loop thru the given Parameters
for ($i=0; $i<count($Params);$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);
}
else
{
$Types = '';
if(is_int($Params)) //Int
$Types .= 'i';
elseif (is_float($Params)) // Double
$Types .= 'd';
elseif (is_string($Params)) // String
$Types .= 's';
else // Blob and Unknown
$Types .= 'b';
$Statment->bind_param($Types ,$Params);
}

return $Statment;
}



دوباره اصلاحاتی انجام دادم برای یک شرط خوب کار می کنه ولی وقتی یه شرط چندتایی واد می کنیم نه


SelectByOrderCondi('user','username=? AND name=? AND email=? ' , $Array )