php - render data via PDO::FETCH_FUNC vs loop -


i've been using 2 methods render data.

the first one:

function name($id,$name){     return '<div id="'.$id.'">'.$name.'</div>'; } echo implode($pdo->query("select id,name user")->fetchall(pdo::fetch_func,'name')); 

the second one:

$users = $pdo->query("select id,name user")->fetchall(pdo::fetch_obj); foreach($users $user){     echo name($user->id,$user->name); } 

i don't understand how pdo::fetch_func works. tried figure out. however, not well-documented.

could please explain fetch mode? , also, 1 performs better? thank you.

both methods wrong , have learn how use templates , how separate business logic presentation logic.

$users = $pdo->query("select id,name user")->fetchall(pdo::fetch_obj); tpl::assign('users', $users); 

is code business logic part.

then in template

<?php foreach $users $row): ?>     <div id="<?=$row->id?>"><?=$row->name?></div> <?php endforeach ?> 

frankly, business logic should contain not trace of html while presentation logic should contain not single database call.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -