Query should return multiple rows, but only one row is returned

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raaman rai
    New Member
    • Oct 2007
    • 114

    Query should return multiple rows, but only one row is returned

    Pls see the code below, where i wanted to select all the records from my table where the submdate (submission date) is greater or equal to today's date. Whereas i have two or three records in my table which has this date greater than today but still then i get only one record displayed. I dont find any problem anywhere but still then i dont get the result displayed as i wanted. Pls help me where i have the problem.

    Code:
     <?php
    global $database;
    $today=date('Y-m-d', mktime());
    		        
    $q = "SELECT * FROM tbltenderinfo where submdate>='".$today."'";	
    		   
    $result = $database->query($q);
             
    $num_rows = mysql_numrows($result);
    
    if(!$result || ($num_rows < 0)){
       echo "Error displaying info";
       return;
     }
    if($num_rows == 0){
       echo "No tenders info available at present";
       return;
    } 
      for($i=0; $i<$num_rows; $i++){
        $tenderID  = mysql_result($result,$i,"tenderID");
        $tenderdesc = mysql_result($result,$i,"tenderdesc");
        $agency  = mysql_result($result,$i,"agency");
        $subdate   = $session->formatdate(mysql_result($result,$i,"submdate")); ?>
    
          <table class="tblstyle" align="right">
            <tr>
              <td class="thead">Sl #</td>
              <td class="thead">Name of Work</td>
              <td class="thead">Procuring Agency</td>
              <td class="thead">Submission Date</td>
              <td class="thead">Details</td>
            </tr>
            <tr>
              <td height="27" class="details"><?php echo $i; ?></td>
              <td class="details"><?php echo $tenderdesc; ?></td>
              <td class="details"><?php echo $agency; ?></td>
              <td class="details"><?php echo $subdate; ?></td>
              <td class="details"><a href="moredetails.php?id=<?php echo $tenderID; ?>&&work=<?php echo $tenderdesc; ?>">More Details</a></td>
           </tr>
        </table>
    <?php
    }
    ?>
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Use mysql_fetch_arr ay instead. That's what is recommended in the manual.

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      [CODE=php]<?php

      /**** Modified By Dan Murad 07/31/2008 ****/

      # Database Info
      global $database;
      $today=date('Y-m-d', mktime());

      # Build Query
      $q = 'SELECT * FROM tbltenderinfo where submdate>= NOW()';

      # Run Query
      $result = $database->query($q);

      # Check Result Set
      if(!$result)
      {
      echo 'Error displaying info.';
      exit();
      }

      # Get Data
      $data = mysql_fetch_ass oc($result);

      # Check Data
      if(empty($data) )
      {
      echo 'No tenders info available at present';
      exit();
      }

      # TABLE HEADER
      echo "<table class='tblstyle ' align='right'>
      <tr>
      <td class='thead'>S l #</td>
      <td class='thead'>N ame of Work</td>
      <td class='thead'>P rocuring Agency</td>
      <td class='thead'>S ubmission Date</td>
      <td class='thead'>D etails</td>
      </tr>
      ";
      # TABLE ROWS
      $counter = 0;
      foreach ($data AS $record)
      {
      echo " <tr>
      <td height='27' class='details' >" , $counter++ , "</td>
      <td class='details' >" , $record['tenderdesc'] , "</td>
      <td class='details' >" , $record['agency'] , "</td>
      <td class='details' >" , $record['submdate'] , "</td>
      <td class='details' ><a href='moredetai ls.php?id=" , $record['submdate'] , "&&work=" , $record['tenderdesc'] , ">More Details</a></td>
      </tr>";

      }

      # REST OF PAGE
      echo '</table>';

      ?> [/CODE]

      Sorry if there's any typography errors.

      Good Luck,



      Dan

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        @Dan, surely you know how to use code tags like the OP did there.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          That doesn't really look right Dan.
          Looks like you only fetch the first row via the mysql_fetch_arr ay function, and then loop through that single row, trying to use it's columns as rows.

          The standard method of looping through database results is something like:
          [code=php]
          $result = mysql_query($qu ery);

          echo "<table>";
          while($row = mysql_fetch_arr ay($result)) {
          echo "<tr>";
          echo " <td>". $row['column1'] ."</td>";
          echo " <td>". $row['column2'] ."</td>";
          echo " <td>". $row['columnN'] ."</td>";
          echo "</tr>";
          }
          echo "</table>";
          [/code]
          You could of course use a foreach loop in there to print the columns, but that's rarely ever useful in a real situation.

          Comment

          Working...