【问题标题】:Wht the php query return a single row什么 php 查询返回单行
【发布时间】:2013-11-30 00:32:01
【问题描述】:

我正在开发一个离线聊天应用程序,我有两个表 1. 用户详细信息(cli_id、电子邮件、用户名) 2. 聊天表(c_from、c_to、主题、事项、图像)现在的问题是我我从用户表中获取 cli_id 作为 from 和 to 但是在获取查询时它返回单行,我的代码看起来像这样

<table width="100%" border="0">
  <tr>
    <td width="16%"><strong>From</strong></td>
    <td width="23%"><strong>Subject</strong></td>
    <td width="40%"><strong>Matter</strong></td>
    <td width="21%"><strong>To</strong></td>
  </tr>
  <?php
    include('connect.php');
    $sql=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
  while($row=mysql_fetch_array($sql))
  {
   ?>
  <tr>
    <td><?php echo $row['c_from']; ?></td>
    <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['subject']; ?></a></td>
    <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['matter']; ?></a></td>
    <td><?php
$chat_to =$row['c_to'];
    $sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
  while($qry=mysql_fetch_array($sql))
  {
  echo $qry['email'];
  }
   ?></td>
  </tr>

  <?php } ?>
    </table>

【问题讨论】:

  • 对嵌套循环中的不同查询结果集使用相同的变量名(例如 $sql)不是如果您不希望值被覆盖

标签: php html mysql sql


【解决方案1】:

您正在循环内部覆盖$sql,这会将外部循环中的结果集替换为在代码执行返回到外部循环时已经“清空”的结果集。

【讨论】:

    【解决方案2】:

    $sql 变量在 while 循环内发生了变化。在这里使用不同的变量:

    $sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <table width="100%" border="0">
        <tr>
          <td width="16%"><strong>From</strong></td>
          <td width="23%"><strong>Subject</strong></td>
          <td width="40%"><strong>Matter</strong></td>
          <td width="21%"><strong>To</strong></td>
        </tr>
        <?php
          include('connect.php');
          $selectChat=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
        while($row=mysql_fetch_array($selectChat))
        {
         ?>
        <tr>
          <td><?php echo $row['c_from']; ?></td>
          <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['subject']; ?></a></td>
          <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['matter']; ?></a></td>
          <td><?php
      $chat_to =$row['c_to'];
          $selectClient=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
        while($qry=mysql_fetch_array($selectClient))
        {
        echo $qry['email'];
        }
         ?></td>
        </tr>
      
        <?php } ?>
          </table>
      

      【讨论】:

        【解决方案4】:

        使用连接可能会更好,以最大限度地减少数据库请求的数量,这也将减少您在第一个循环内有第二个查询循环的需要。试试下面的代码

        <table width="100%" border="0">
        <tr>
        <td width="16%"><strong>From</strong></td>
        <td width="23%"><strong>Subject</strong></td>
        <td width="40%"><strong>Matter</strong></td>
        <td width="21%"><strong>To</strong></td>
        </tr>
        <?php
        include('connect.php');
        $sql=mysql_query("SELECT * FROM `chat` LEFT JOIN 'client' on 'chat.c_to = client.cli_id' ORDER BY chat_id DESC")or die(mysql_error());
        while($row=mysql_fetch_array($sql))
        {
        ?>
        <tr>
        <td><?php echo $row['c_from']; ?></td>
        <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['subject']; ?></a>  </td>
        <td><a href="read_chat.php?id=<?php echo $row['chat_id']; ?>"><?php echo $row['matter']; ?></a></td>
        <td><?php echo $row['email'];?></td>
        </tr>
        
        <?php } ?>
        </table>
        

        【讨论】:

          【解决方案5】:

          你必须重写紧跟在主查询之后出现的while语句

          while($row=mysql_fetch_array($sql))
          

          作为

          while($row=mysql_fetch_row($sql))
          

          希望这对你有帮助。

          【讨论】:

          • 不太可能有帮助,实际上会在代码中触发更多错误,因为行值是按列名而不是数字索引访问的
          猜你喜欢
          • 2023-04-10
          • 2016-03-21
          • 2011-12-04
          • 2012-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多