douzhi3105 2012-08-02 20:50
浏览 40

返回数组中的MySQL行

I've been working on a OOP method that is supposed to return the rows of a MySQL query. I have to have the data array in the format:

$rows = array('row'=> rownum, 'fld1'=> fldval .... 'fldn' => fldval);

What I have encountered are the two problems of either:

returns

$rows = array('0'=>fldval, 'fld1'=> fldval .... 'n'=> fldval, 'fldn' => fldval);

or single row of

$rows = array('fld1'=> fldval .... 'fldn' => fldval);

Little frustrated as every PHP mysql function I have tried has some sort to goofy crap flaw and will not do a straight out process.

I assume there is a good example somewhere, that can get me past the crap limitations, but haven't found anything useful yet!

I've tried all of the following:

$row = mysql_result($db_res,$n);
$row = mysql_fetch_array($db_res);
$row = mysql_fetch_assoc($db_res);
$row = mysql_fetch_object($db_res);
$row = mysql_fetch_row($db_res);  

None have worked successfully! For getting out the bogus "numeric" array entries. I wrote:

foreach ($row as $k => $v)
   if (is_numeric($k)) { continue; }
   $result[$k] = $v;
}  // end foreach $row
$row = array_push($row, 'row'=>$rownum, $result);

Hoping someone has a link.

  • 写回答

1条回答 默认 最新

  • doom910730 2012-08-02 21:15
    关注
    $list = array();
    $query = "SELECT value FROM table";
    $resource = mysql_query($query);
    while($row = mysql_fetch_assoc($resource))
    {
       $list['fld' . (1 + count($list))] = $row['value'];
    }
    $list = array('row' => count($list)) + $list;
    

    if table have 3 row, the code above is going to give you a array like:

    array(
        'row' => 3,
        'fld1' => 12,
        'fld2' => 34,
        'fld3' => 56
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度