我有過一組數據的循環的腳本,並調用一個函數,而這樣做:PHP - 添加到陣列和循環變量
$d = $dbh->prepare("
SELECT *
FROM xeon_users_rented
WHERE since <= unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 14 day)
AND clicks_last <= unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 14 day)
");
$d->execute();
$rows = $d->fetchAll(PDO::FETCH_ASSOC);
$new_array = array();
foreach ($rows as $data) {
$new_array[] = $data['id'];
$userToRecycleFor = $data['user_by'];
$outcome = $rentedrefs->_recycleMulti(
0,
$userToRecycleFor,
$new_array,
1
);
}
這是我的功能_recycleMulti();
:
function _recycleMulti($ceny, $referer, $recycle_array, $free=false){
global $dbh;
$this->ceny['rec'] = $ceny;
$totalrecycle = count($recycle_array);
$koszyk = $this->ceny['rec'] * count($recycle_array);
$referer_sql = $dbh->prepare("
SELECT *
FROM `users`
WHERE `username` = :referer
LIMIT 1;
");
$referer_sql->bindParam(':referer', $referer);
try {
$referer_sql->execute();
}
catch (PDOException $e) {
_OP_ERROR($e->getMessage(), __FILE__, __LINE__);
}
$referer_dane = $referer_sql->fetch();
$referer_dane_accont = $referer_dane['membership'];
for($i = 0; $i < count($recycle_array); $i++){
# Expires
$recycle_sql = $dbh->prepare("
SELECT *
FROM `users_rented`
WHERE `id` = :id
LIMIT 1;
");
$recycle_sql->bindParam(':id', $recycle_array[$i]);
$recycle_sql->execute();
$recycle = $recycle_sql->fetch();
echo "row count:";
echo $recycle_sql->rowCount();
if($recycle_sql->rowCount() == 0){
return false;
}
if(! $recycle_sql
|| ($recycle_sql->rowCount() == 0)
|| $recycle['user_by'] != $referer_dane['username']){
return false;
}
return true;
}
現在,我的問題是,它運行良好的功能。儘管它只是第一次運行,但它起作用。我已經加入這個:
echo "row count:";
echo $recycle_sql->rowCount();
這是結果:
row count:1row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0row count:0
正如你所看到的,這只是第一次它正在運行,有一個有效行。
我在做什麼錯?
第一印象:在每次循環迭代中,使用相同的字符串製作一個新的準備好的語句似乎是一個壞主意。 – 2014-11-02 17:43:41
你可以檢查'$ recycle = $ recycle_sql-> fetch();'是否返回? – puelo 2014-11-02 17:47:34
@puelo它包含'users'表中的數據 – oliverbj 2014-11-02 17:49:36