如何使用 ODBC 将行保存到数组并在 PHP 中打印出来?

发布于 2024-08-27 23:16:32 字数 892 浏览 4 评论 0原文

我有以下内容:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    $thisResult['name'] = $myRow["name"] ;
    $thisResult['race'] = $myRow["race"] ;
    $thisResult['sex'] = $myRow["sex"];
    $thisResult['dob'] = $myRow["dob"];
}

我不知道如何打印出来。

我想像数据读取器一样获取每一行并迭代数组中的每一行。我不知道该怎么办。我暂时不想做回声。我需要能够在其他地方打印出来。但我不认为我在这里这样做是为了以后能够打印它。

我也尝试过,但是:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
     print($thisResult[$myRow["name"]] = $myRow);
}

然后我尝试了:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    print (odbc_result($myRow,"name"));
}

但出现错误。

感谢您的帮助。

编辑:当我这样做时:

while($myRow = odbc_fetch_array( $result )){
    print ($myRow["name"]);
}

我得到未定义的索引名称。我主要关心的是保存到数组,但我必须能够首先在循环中完成它。

I have the following:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    $thisResult['name'] = $myRow["name"] ;
    $thisResult['race'] = $myRow["race"] ;
    $thisResult['sex'] = $myRow["sex"];
    $thisResult['dob'] = $myRow["dob"];
}

I can't figure out how to print this back out.

I want to get each row and iterate through each row in the array like a datareader. I'm not sure what to do. I do not want to do the echo in the while. I need to be able to print it out elsewhere. But I don't think I've done it right here to be able to print it later.

I also tried, this, however:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
     print($thisResult[$myRow["name"]] = $myRow);
}

I then tried:

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    print (odbc_result($myRow,"name"));
}

but got an error.

Thank you for any help.

EDIT: when I do this:

while($myRow = odbc_fetch_array( $result )){
    print ($myRow["name"]);
}

I get undefined index name. I am mainly concerned with saving to an array but I have to be able to do it in the loop first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-09-03 23:16:32

之前声明一个数组并为其分配值:

$rows = array();

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    $rows[] = $myRow;
}

然后您可以这样打印它:

foreach($rows as $row) {
   foreach($row as $key => $value) {
       echo $key . ': '. $value;
   }
}

或者您想要的任何方式。

您不必在 while 循环中访问并分配 $thisResult['name'] = $myRow["name"] 作为 $myRow 已经是一个数组。您只需复制不必要的值即可。


你说你有很多行。根据您真正想要对数据执行的操作,最好将所有这些功能放入 while 循环中以避免创建数组。

Declare an array before and assign the values to it:

$rows = array();

while($myRow = odbc_fetch_array( $result )){ <--lots of rows
    $rows[] = $myRow;
}

Then you can print it e.g. this way:

foreach($rows as $row) {
   foreach($row as $key => $value) {
       echo $key . ': '. $value;
   }
}

or however you want to.

You don't have to access and assign $thisResult['name'] = $myRow["name"] in your while loop as $myRow already is an array. You just copy the values which is unnecessary.


You say you have a lot of rows. Depending of what you really want to do with data, it might be better to put all this functionality into the while loop to avoid creating an array.

吹泡泡o 2024-09-03 23:16:32

怎么样:

$output = '';
while($myRow = odbc_fetch_array( $result )) {
    $output = $output."Your name is {$myRow["name"]} and your race is {$myRow["race"]}\n";
}

// print output later...

How about something like:

$output = '';
while($myRow = odbc_fetch_array( $result )) {
    $output = $output."Your name is {$myRow["name"]} and your race is {$myRow["race"]}\n";
}

// print output later...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文