简体   繁体   English

返回数组中的MySQL行

[英]Return MySQL rows in array

I've been working on a OOP method that is supposed to return the rows of a MySQL query. 我一直在研究一个OOP方法,该方法应该返回MySQL查询的行。 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. 有点沮丧,因为我尝试过的每个PHP mysql函数都有一些愚蠢的废话,并且不会直接进行。

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. 希望有人有链接。

$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: 如果表有3行,上面的代码将为您提供如下数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM