如何使用 ODBC 将行保存到数组并在 PHP 中打印出来?
我有以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之前声明一个数组并为其分配值:
然后您可以这样打印它:
或者您想要的任何方式。
您不必在
while
循环中访问并分配$thisResult['name'] = $myRow["name"]
作为$myRow 已经是一个数组。您只需复制不必要的值即可。
你说你有很多行。根据您真正想要对数据执行的操作,最好将所有这些功能放入 while 循环中以避免创建数组。
Declare an array before and assign the values to it:
Then you can print it e.g. this way:
or however you want to.
You don't have to access and assign
$thisResult['name'] = $myRow["name"]
in yourwhile
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.
怎么样:
How about something like: