mysqli_fetch_array()foreach循环重复 [英] mysqli_fetch_array() foreach loop duplicates

查看:145
本文介绍了mysqli_fetch_array()foreach循环重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对返回数组的数据库执行请求。我使用了一个 foreach 循环来检查这些值。

数据库只有1条)

  id | field1 | field2 | field3 | field4 | field5 
--- | -------- | -------- | -------- | -------- | ----- ---
1 | value1 | | value3 | value4 | value5

PHP源代码

  $ mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME); 
$ q = mysqli_query($ mysqli,SELECT * FROM my_table);

echo< ul>;
while $($ q = mysqli_fetch_array($ q))
{
foreach($ q as $ key => $ value)
{
if($ value != NULL)
{
echo< li>。$ value。 < /锂> 中;

//下面的行用于显示列名和相应的值
// echo< li>< strong> {$ key}< / strong> /< em> {$值}< / EM>< /锂>中;
}
}
}
echo< / ul>;

预期的HTML输出

 < ul> 
< li> value1< / li>
< li> value3< / li>
< li> value4< / li>
< li> value5< / li>
< / ul>

当前输出

 < UL> 
< li> 1< / li>
< li> 1< / li>
< li> value1< / li>
< li> value1< / li>
< li> value3< / li>
< li> value3< / li>
< li> value4< / li>
< li> value4< / li>
< li> value5< / li>
< li> value5< / li>
< / ul>

当前问题

正如你所看到的,我目前拥有所有的东西都是双倍的,出于一些奇怪的原因,我得到了行ID,所以我假设考虑当我向数据库添加一个新行时没有第二行。
$ b

替代代码



使用注释行,我得到这个输出:

 < ul> 
< li> 0/1< / li>
< li> id / 1< / li>
< li> 1 / value1< / li>
< li> field1 / value1< / li>
< li> 3 / value3< / li>
< li> field3 / value3< / li>
< li> 4 / value4< / li>
< li> field4 / value4< / li>
< li> 5 / value5< / li>
< li> field5 / value5< / li>
< / ul>

根据我的理解




  • 一个 while 循环可以让我遍历数据库中的每一行

  • foreach 循环类似于,而循环,但不是通过数据库条目,而是通过键/值对来自数组



如果我对这些前面的陈述有错误的话,请纠正我的错误。)



大声思考 b $ b


  • 我开始怀疑,当我写这个问题时,如果 while foreach
  • 行号(在这种情况下 1 )是一个关键吗?

    解决方案

    默认情况下, mysqli_fetch_array()返回关联数组( $ key => $ value )和一个数字数组( 0 =>值),这就是为什么你得到重复 - 一些条目有列名作为键,其他的有一个数字索引。

    如果你想要一个关联数组,你可以使用:

      mysqli_fetch_array($ q,MYSQLI_ASSOC); 
    //或
    mysqli_fetch_assoc($ q);

    或者对于一个数字数组:

      mysqli_fetch_array($ q,MYSQLI_NUM); 
    //或
    mysqli_fetch_row($ q);

    更多信息: http://www.php.net/manual/en/mysqli-result.fetch-array.php


    I'm performing a request on a database which returns an array. I'm using a foreach loop to go through the values.

    Database (only 1 entry)

    id | field1 | field2 | field3 | field4 | field5
    ---|--------|--------|--------|--------|--------
    1  | value1 |        | value3 | value4 | value5
    

    PHP Source code

    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $q = mysqli_query($mysqli, "SELECT * FROM my_table");
    
    echo "<ul>";
        while($q = mysqli_fetch_array($q))
        {
            foreach ($q as $key => $value)
            {
                if($value != NULL)
                {
                    echo "<li>".$value . "</li>";
    
                    // Line below used to show column name and corresponding value
                    //echo "<li><strong>{$key}</strong> / <em>{$value}</em></li>";
                }
            }
        }
    echo "</ul>";
    

    Expected HTML output

    <ul>
        <li>value1</li>
        <li>value3</li>
        <li>value4</li>
        <li>value5</li>
    </ul>
    

    Current output

    <ul>
        <li>1</li>
        <li>1</li>
        <li>value1</li>
        <li>value1</li>
        <li>value3</li>
        <li>value3</li>
        <li>value4</li>
        <li>value4</li>
        <li>value5</li>
        <li>value5</li>
    </ul>
    

    Current issue

    As you can see, I current have everything in double, and for some wierd reason, I'm getting the row ID, or so I assume considering I do not get a second row when I add a new row to the database.

    Alternate code

    Using the commented line, I get this output:

    <ul>
        <li>0 / 1</li>
        <li>id / 1</li>
        <li>1 / value1</li>
        <li>field1 / value1</li>
        <li>3 / value3</li>
        <li>field3 / value3</li>
        <li>4 / value4</li>
        <li>field4 / value4</li>
        <li>5 / value5</li>
        <li>field5 / value5</li>
    </ul>
    

    From what I understand

    • A while loop would allow me to go through each row from the database.
    • A foreach loop is similar to a while loop, but instead of going through database entries, it goes through the key/value pairs from an array.

    (Please feel free to correct me if I am wrong about these previous statements.)

    Thinking outloud

    • I'm starting to wonder, as I write this question, if the while and foreach seem kind of redundant in that both seem would parse the values, probably causing my duplicates...?
    • Wouldn't the row number (in this case 1) be a key?

    解决方案

    By default, mysqli_fetch_array() returns both an Associative Array ($key => $value) and a Numeric Array (0 => value), which is why you're getting duplicates - some of the entries have the column name as the key, the others have a numeric index.

    If you want an associative array, you could use:

     mysqli_fetch_array($q, MYSQLI_ASSOC);
     //or
     mysqli_fetch_assoc($q);
    

    Or for a numeric array:

     mysqli_fetch_array($q, MYSQLI_NUM);
     //or
     mysqli_fetch_row($q);
    

    For more info: http://www.php.net/manual/en/mysqli-result.fetch-array.php

    这篇关于mysqli_fetch_array()foreach循环重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆