【问题标题】:Why this don't work? [closed]为什么这不起作用? [关闭]
【发布时间】:2017-12-13 05:33:17
【问题描述】:

这个功能是有效的:

function son_bolum_liste_cek($limit)
{   
global $db;
$query = $db->prepare("SELECT `anime_bolum`.`id`, `anime`.`isim`, `anime_bolum`.`bolum_no`, `image`.`url` as `image`, `gifimage`.`url` as `gifimage` FROM `anime_bolum` JOIN `anime` ON `anime`.`id` = `anime_bolum`.`anime_id` JOIN `image` ON `anime_bolum`.`image` = `image`.`id` JOIN `image` as `gifimage` ON `gifimage`.`id` = `anime_bolum`.`gifimage` ORDER BY `anime_bolum`.`id` DESC LIMIT :limit;");
$query->bindValue(':limit', $limit, PDO::PARAM_INT);
$select = $query->execute();
return $query->fetchAll();
}
foreach (son_bolum_liste_cek(12) as $anime){blabla};

但这是行不通的:

function db_toplu_veri_oku($query, $execute = array() , $binds = array(),$debug = false)
{
global $db;
$query = $db->prepare($query);
foreach($binds as $bind)
    {
    $query->bindValue($bind[0], $bind[1], $bind[2]);
    }

$select = $query->execute($execute);
if($debug){$query->debugDumpParams();};
return $query->fetchAll();
}

function son_bolum_liste_cek($limit)
{   
$query = "SELECT `anime_bolum`.`id`, `anime`.`isim`, `anime_bolum`.`bolum_no`, `image`.`url` as `image`, `gifimage`.`url` as `gifimage` FROM `anime_bolum` JOIN `anime` ON `anime`.`id` = `anime_bolum`.`anime_id` JOIN `image` ON `anime_bolum`.`image` = `image`.`id` JOIN `image` as `gifimage` ON `gifimage`.`id` = `anime_bolum`.`gifimage` ORDER BY `anime_bolum`.`id` DESC LIMIT :limit;";
$binds = array(array(':limit',$limit,PDO::PARAM_INT));
return db_toplu_veri_oku($query,array(),$binds);
}
foreach (son_bolum_liste_cek(12) as $anime){blabla};

两个功能做同样的工作,但底部功能不工作,为什么?我哪里做错了?

【问题讨论】:

  • 定义“不工作”。它应该做什么以及它是如何失败的?
  • 底部函数返回空数组,但上部不返回空数组。我不想返回空数组。(我不能用英语告诉自己:@@)

标签: php mysql pdo


【解决方案1】:

变化不大。问题在execute。您在此传递空白数组。所以你必须先检查数组是否包含值

function db_toplu_veri_oku($query, $execute = array() , $binds = array(),$debug = false)
{
global $db;
$query = $db->prepare($query);
foreach($binds as $bind)
    {
    $query->bindValue($bind[0], $bind[1], $bind[2]);
    }

if(count($execute)) //<-----------Add this condition
    $select = $query->execute($execute);
else
    $select = $query->execute();
if($debug){$query->debugDumpParams();};
return $query->fetchAll();
}

【讨论】:

  • 谢谢这是工作!
猜你喜欢
  • 2014-09-12
  • 1970-01-01
  • 2015-12-08
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
相关资源
最近更新 更多