我对从 MySQL 获取的数组 $row 有一些问题

发布于 2024-12-23 01:34:20 字数 422 浏览 1 评论 0原文

我对从 MySQL 获取的数组有一些问题

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
mysql_fetch_row($result);

echo $row[0]; // doesn't work
echo $row[1]; // doesn't work

,但是这项工作

echo $row["FirstFieldName"] //OK
...

我应该如何更改以下代码才能使其工作?

for ( $i = 0; $i < count( $row ); $i++ )
{
    echo $row[ $i ];
}

谢谢

I've some issues with an array fetched from MySQL

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
mysql_fetch_row($result);

echo $row[0]; // doesn't work
echo $row[1]; // doesn't work

but this work

echo $row["FirstFieldName"] //OK
...

how should I change the following code to make it work?

for ( $i = 0; $i < count( $row ); $i++ )
{
    echo $row[ $i ];
}

thanks

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

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

发布评论

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

评论(5

源来凯始玺欢你 2024-12-30 01:34:20

进行以下更改。使用mysql_fetch_array代替mysql_fetch_row()

mysql_fetch_row()从与指定结果标识符关联的结果中获取一行数据。该行作为数组返回。每个结果列都存储在一个数组偏移量中,从偏移量 0 开始。

 $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");

$row=mysql_fetch_array($result);

for ( $i = 0; $i < count( $row ); $i++ )
{
    echo $row[ $i ];
}

Do below changes. use mysql_fetch_array instead of mysql_fetch_row()

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

 $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");

$row=mysql_fetch_array($result);

for ( $i = 0; $i < count( $row ); $i++ )
{
    echo $row[ $i ];
}
水中月 2024-12-30 01:34:20

尝试使用 mysql_fetch_array() 代替。

您应该研究的三个函数是:

mysql_fetch_row、
mysql_fetch_array 和
mysql_fetch_assoc

每个的做法都略有不同。

试试这样:

$sql = "SELECT id,email FROM people WHERE id = '42'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
  echo $row[0];
}

Try using mysql_fetch_array() instead.

The three functions you should look into are:

mysql_fetch_row,
mysql_fetch_array, and
mysql_fetch_assoc

Each does things a little differently.

Try it this way:

$sql = "SELECT id,email FROM people WHERE id = '42'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
  echo $row[0];
}
假扮的天使 2024-12-30 01:34:20
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row=mysql_fetch_array($result);

echo $row[0];
echo $row[1];

会起作用的

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row=mysql_fetch_array($result);

echo $row[0];
echo $row[1];

it will work

梦里寻她 2024-12-30 01:34:20

mysql_fetch_row 返回一行,因此为了能够使用它,您应该这样做:

while( $row = mysql_fetch_row( $result ) ) 
{
    echo $row["email"];
}

否则,正如 Mike 所说,您应该使用 mysql_fetch_array()

mysql_fetch_row returns one row, so to be able to use it you should do :

while( $row = mysql_fetch_row( $result ) ) 
{
    echo $row["email"];
}

Otherwise, as Mike said you should use mysql_fetch_array()

策马西风 2024-12-30 01:34:20

即....这将获取所有检索到的键/字段并对它们进行交互并将它们交给$s,然后可以将其放入html中..

foreach($row as $key=>$val){
      $key = $val;
      $s.= "".$key."
      <输入类型=文本大小=88 name='".$key."' value='".$val."'>";
      }
// 结束你的 php 然后继续 html
    <table>
    <?php echo $s;?>
    </table>

ie.... this takes all the retrieved keys/fields and intereates them and hands them off to $s which can then be placed into html..

foreach($row as $key=>$val){
      $key = $val;
      $s.= "<tr><td>".$key."</td><td>
      <input type=text size=88 name='".$key."' value='".$val."'></td></tr>";
      }
// end your php then onward to html
    <table>
    <?php echo $s;?>
    </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文