php循环取一条数据 php循环取json数组对象( 二 )


return (odbc_exec($this-link,$sql) or $this-display_error($sql));
显然,每次都display_error了,不管是否成功
这是函数odbc_fetch_row的定义:
odbc_fetch_row
(PHP 4, PHP 5)
odbc_fetch_row — Fetch a row
Description
bool odbc_fetch_row ( resource $result_id [, int $row_number] )
If odbc_fetch_row() was successful (there was a row), TRUE is returned. If there are no more rows, FALSE is returned.
odbc_fetch_row() fetches a row of the data that was returned by odbc_do() / odbc_exec(). After odbc_fetch_row() is called, the fields of that row can be accessed with odbc_result().
If row_number is not specified, odbc_fetch_row() will try to fetch the next row in the result set. Calls to odbc_fetch_row() with and without row_number can be mixed.
To step through the result more than once, you can call odbc_fetch_row() with row_number 1, and then continue doing odbc_fetch_row() without row_number to review the result. If a driver doesn't support fetching rows by number, the row_number parameter is ignored.
可以看到,odbc_fetch_row并不像其它函数例如mysql_fetch_row那样返回一个数组,而是返回一个布尔型的变量,然后在用odbc_result获取指定字段的值 。
修改如下:
?
$databasepath="database.mdb";//写成常量,define('databasepath',"database.mdb");赋值给变量$databasepath=databasepath;
$dbusername="";
$dbpassword="";
$access=new Access($databasepath,$dbusername,$dbpassword,true);
$conn=$access-query("select * from admin");
while (odbc_fetch_row($conn)) {
?
tr
td class="td1"?=odbc_result($conn, "name")?/td
/tr
tr
td class="td1"?=odbc_result($conn, "area")?/td
/tr
?php
}
$access-close();
?
请下载PHP参考手册
如何获取php foreach循环出来的其中一条数据foreach
语法结构提供了遍历数组的简单方式 。foreach
仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量 , 或者未初始化的变量将发出错误信息 。有两种语法:
foreach
(array_expression
as
$value)
statement
foreach
(array_expression
as
$key
=
$value)
statement
第一种格式遍历给定的
【php循环取一条数据 php循环取json数组对象】array_expression
数组 。每次循环中,当前单元的值被赋给
$value
并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元) 。
第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量
$key 。
范例:
$a
=
array(
"one"
=
1,
"two"
=
2,
"three"
=
3,
"seventeen"
=
17);foreach
($a
as
$k
=
$v)
{
echo
"\$a[$k]
=
$v.\n";}
关于php循环取一条数据和php循环取json数组对象的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读