Php 为什么循环不止一次…我错过了什么吗

Php 为什么循环不止一次…我错过了什么吗,php,foreach,Php,Foreach,表中只有一条记录,为什么它会像我有5个表一样循环,每个表有一个字母 $query = "Select * from click_tracker"; $result = mysql_query($query); $all_clicks = mysql_fetch_array($result); foreach($all_clicks as $click){ print " <table border=\"1\"> <tr> &l

表中只有一条记录,为什么它会像我有5个表一样循环,每个表有一个字母

$query = "Select * from click_tracker";
$result = mysql_query($query);
$all_clicks = mysql_fetch_array($result);

foreach($all_clicks as $click){
    print "
    <table border=\"1\">
      <tr>
        <th>Location</th>
        <th>Visit Count</th>
      </tr>
      <tr>
        <td>{$click['url_destination']}</td>
        <td>{$click['count']}</td>
      </tr>
    </table>";
}
$query=“从单击中选择*”;
$result=mysql\u query($query);
$all\u clicks=mysql\u fetch\u数组($result);
foreach($click表示所有点击){
“打印”
位置
访问次数
{$click['url\u destination']}
{$click['count']}
";
}
这是归还的桌子

<table border="1">
  <tr>
    <th>Location</th>

    <th>Visit Count</th>
  </tr>
  <tr>
    <td>2</td>
    <td>2</td>
  </tr>
</table>

<table border="1">
  <tr>
    <th>Location</th>
    <th>Visit Count</th>
  </tr>
  <tr>
    <td>2</td>

    <td>2</td>
  </tr>
</table>
<table border="1">
  <tr>
    <th>Location</th>
    <th>Visit Count</th>

  </tr>
  <tr>
    <td>h</td>
    <td>h</td>
  </tr>
</table>
<table border="1">
  <tr>

    <th>Location</th>
    <th>Visit Count</th>
  </tr>
  <tr>
    <td>h</td>
    <td>h</td>
  </tr>

</table>
<table border="1">
  <tr>
    <th>Location</th>
    <th>Visit Count</th>
  </tr>
  <tr>
    <td>5</td>

    <td>5</td>
  </tr>
</table>
<table border="1">
  <tr>
    <th>Location</th>
    <th>Visit Count</th>

  </tr>
  <tr>
    <td>5</td>
    <td>5</td>
  </tr>
</table>   

位置
访问次数
2.
2.
位置
访问次数
2.
2.
位置
访问次数
H
H
位置
访问次数
H
H
位置
访问次数
5.
5.
位置
访问次数
5.
5.

您需要这样做:

$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {

您需要这样做:

$query = "Select * from click_tracker";
$result = mysql_query($query);
while($click = mysql_fetch_assoc($result)) {
执行
print\r($all\u clicks)
并检查结果是否符合预期

如果只有一个结果,那么实际上不需要使用foreach

 $query = "Select * from click_tracker";
    $result = mysql_query($query);
    $all_clicks = mysql_fetch_array($result);
     print "
        <table border=\"1\">
          <tr>
            <th>Location</th>
            <th>Visit Count</th>
          </tr>
          <tr>
            <td>{$all_clicks['url_destination']}</td>
            <td>{$all_clicks['count']}</td>
          </tr>
        </table>";
$query=“从单击中选择*”;
$result=mysql\u query($query);
$all\u clicks=mysql\u fetch\u数组($result);
“打印”
位置
访问次数
{$all_clicks['url_destination']}
{$all_点击次数['count']}
";
进行
打印($all\u clicks)
并检查结果是否符合预期

如果只有一个结果,那么实际上不需要使用foreach

 $query = "Select * from click_tracker";
    $result = mysql_query($query);
    $all_clicks = mysql_fetch_array($result);
     print "
        <table border=\"1\">
          <tr>
            <th>Location</th>
            <th>Visit Count</th>
          </tr>
          <tr>
            <td>{$all_clicks['url_destination']}</td>
            <td>{$all_clicks['count']}</td>
          </tr>
        </table>";
$query=“从单击中选择*”;
$result=mysql\u query($query);
$all\u clicks=mysql\u fetch\u数组($result);
“打印”
位置
访问次数
{$all_clicks['url_destination']}
{$all_点击次数['count']}
";
mysql\u fetch\u array()返回行,看起来您的foreach在一行字段上循环,而不是在结果集中的行。

mysql\u fetch\u array()返回行,看起来您的foreach在一行字段上循环,而不是在结果集中的行。

一行作为数组获取。当您尝试使用
foreach
循环该结果时,实际上是在循环返回的行的所有列(实际上是两次,因为默认情况下,
mysql\u fetch\u array
返回一个包含数字键和索引键的数组!)

如果要获取结果集中的所有行(而且很可能是这样),则需要使用while循环继续获取行,直到不再有行为止:

$all_clicks = array();
while ($row = mysql_fetch_array($result))
{
  $all_clicks[] = $row;
}
然后,当您迭代
$all_clicks
时,每次迭代都将有一个完整的行。

一行作为数组获取。当您尝试使用
foreach
循环该结果时,实际上是在循环返回的行的所有列(实际上是两次,因为默认情况下,
mysql\u fetch\u array
返回一个包含数字键和索引键的数组!)

如果要获取结果集中的所有行(而且很可能是这样),则需要使用while循环继续获取行,直到不再有行为止:

$all_clicks = array();
while ($row = mysql_fetch_array($result))
{
  $all_clicks[] = $row;
}

然后,当您迭代
$all_clicks
时,每次迭代都将有一个完整的行。

您似乎正在打印多个表。我不认为这是你想要的。您需要在循环外部打印表的开始和结束标记以及标题。您还应该在循环中调用
mysql\u fetch\u array()
,而不是只调用一次

print "
    <table border=\"1\">
      <tr>
        <th>Location</th>
        <th>Visit Count</th>
      </tr>";

$query = "Select * from click_tracker";
$result = mysql_query($query);

while ($click = mysql_fetch_array($result)) {
    print "
      <tr>
        <td>{$click['url_destination']}</td>
        <td>{$click['count']}</td>
      </tr>";
}

print "</table>";
打印”
位置
访问次数
";
$query=“从单击中选择*”;
$result=mysql\u query($query);
而($click=mysql\u fetch\u array($result)){
“打印”
{$click['url\u destination']}
{$click['count']}
";
}
打印“”;

你也应该考虑在<代码> >点击“< /代码>”中逃出数据,但我不知道你的数据是什么样子,所以我不确定在“代码< > 和<代码>打印< /COD>语句之间的区域放什么。”/P> < P>你似乎正在打印多个表。我不认为这是你想要的。您需要在循环外部打印表的开始和结束标记以及标题。您还应该在循环中调用

mysql\u fetch\u array()
,而不是只调用一次

print "
    <table border=\"1\">
      <tr>
        <th>Location</th>
        <th>Visit Count</th>
      </tr>";

$query = "Select * from click_tracker";
$result = mysql_query($query);

while ($click = mysql_fetch_array($result)) {
    print "
      <tr>
        <td>{$click['url_destination']}</td>
        <td>{$click['count']}</td>
      </tr>";
}

print "</table>";
打印”
位置
访问次数
";
$query=“从单击中选择*”;
$result=mysql\u query($query);
而($click=mysql\u fetch\u array($result)){
“打印”
{$click['url\u destination']}
{$click['count']}
";
}
打印“”;

你也应该考虑在<代码> > $点击< /代码>中逃出数据,但是我不知道你的数据是什么样子,所以我不确定在“<代码> < <代码> >和<代码>打印< /COD>语句之间的区域中放什么。我相信MySQL LFETCHYLNA数组返回一个数组,因此$ALL点击是一个数组-$结果是结果资源。PHP手册中提到mysql_fetch_array()“返回一个对应于所取行的数组,并将内部数据指针向前移动。”在示例中,

$result
是一个资源,而不是
$all_clicks
您的代码在编辑答案前几秒钟已经阅读了
mysql_fetch_assoc($all_clicks)
。哦,这是真的。是的,那是个打字错误。现在,我明白了@Robin的意思。感谢您的澄清:)这是漫长的一天和午夜:)我相信mysql_fetch_数组返回一个数组,因此$all_clicks是一个数组-$result是一个结果资源。嗯。。。PHP手册中提到mysql_fetch_array()“返回一个与所取行相对应的数组,并将内部数据指针向前移动。”在示例中,
$result
是一个资源,而不是
$all_clicks
您的代码已经读取了
mysql_fetch_assoc($all_clicks