dravpuso44681 2014-03-11 21:41
浏览 57
已采纳

如何在fatfree框架中循环mysql结果集?

I am new to the php fat-free framework, and I am trying figure out how to loop through my mysql query results, or better yet, get it as an associative array (for learning purposes only).

What I did so far is

while(!$users->dry()){
   array_push($user_assoc,$users->cast());
   $users->next();
}

This works, but I was wondering if there is a better way of doing this? Also how do I setup a error handler? I mean how do I check if the query had any errors (i.e. fat-free equivalent of mysql_error())?

  • 写回答

2条回答 默认 最新

  • dongzhang0418 2014-03-12 07:11
    关注

    DB querying

    There are 3 variants to loop through db results:

    Without mapper:

    Execute a SQL query and fetch the result set as an array of associative arrays:

    $users = $db->exec('SELECT * FROM users');
    foreach($users as $user)
      echo $user['name'];//associative array
    

    With mapper->load:

    Fetch mapper rows one by one (your method):

    $user=new \DB\SQL\Mapper($db,'users');
    $user->load('');
    while(!$user->dry()) {
      echo $user->name;//db mapper
      $user->next();
    }
    

    With mapper->find:

    Fetch the result set as an array of mappers:

    $mapper=new \DB\SQL\Mapper($db,'users');
    $users=$mapper->find('');
    foreach($users as $user)
      echo $user->name;//db mapper
    

    DB error handling

    \DB\SQL is a subclass of PDO so it can throw catchable PDO exceptions. Since these are disabled by default, you need to enable them first. This can be done in 2 different ways:

    • at instantiation time, for all transactions:

      $db = new \DB\SQL($dsn, $user, $pwd, array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ));

    • later on in the code, on a per-transaction basis:

      $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

    Once PDO exceptions are enabled, just catch them as other exceptions:

    try {
      $db->exec('INSERT INTO mytable(id) VALUES(?)','duplicate_id');
    } catch(\PDOException $e) {
      $err=$e->errorInfo;
      //$err[0] contains the error code (23000)
      //$err[2] contains the driver specific error message (PRIMARY KEY must be unique)
    }
    

    This also works with DB mappers, since they rely on the same DB\SQL class:

    $db=new \DB\SQL($dsn,$user,$pwd,array(\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION));
    $mytable=new \DB\SQL\Mapper($db,'mytable');
    try {
      $mytable->id='duplicate_id';
      $mytable->save();//this will throw an exception
    } catch(\PDOException $e) {
      $err=$e->errorInfo;
      echo $err[2];//PRIMARY KEY must be unique
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题