mysql - php - add to array and loop variable -
i have script loops through set of data, , phone call function while doing so:
$d = $dbh->prepare(" select * xeon_users_rented since <= unix_timestamp(current_timestamp - interval 14 day) , clicks_last <= unix_timestamp(current_timestamp - interval 14 day) "); $d->execute(); $rows = $d->fetchall(pdo::fetch_assoc); $new_array = array(); foreach ($rows $data ) { $new_array[] = $data['id']; $usertorecyclefor = $data['user_by']; $outcome = $rentedrefs->_recyclemulti( 0, $usertorecyclefor, $new_array, 1 ); }
this function _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 * `users` `username` = :referer limit 1; "); $referer_sql->bindparam(':referer', $referer); seek { $referer_sql->execute(); } grab (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 * `users_rented` `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 ){ homecoming false; } if( ! $recycle_sql || ( $recycle_sql->rowcount() == 0 ) || $recycle['user_by'] != $referer_dane['username'] ){ homecoming false; } homecoming true; }
now, problem is, runs function fine. although first time it's beingness run, works. have added this:
echo "row count:"; echo $recycle_sql->rowcount();
and result:
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
as can see, it's first time it's beingness run, there valid row.
what doing wrong?
the explanation right in documentation of rowcount()
:
if lastly sql statement executed associated pdostatement select statement, databases may homecoming number of rows returned statement. however, behaviour not guaranteed databases , should not relied on portable applications.
in other words, rowcount()
not provide info whether code works.
to check if query successful, check homecoming value of execute()
or fetch()
, both homecoming false
on failure. in latter case, create sure compare using ===
match type. in addition, functions throw exceptions additional information, if query fails. check the examples fetch()
on how handle these exceptions.
apart that, not using result in $recycle
@ all.
php mysql sql pdo
No comments:
Post a Comment