Jump to content

Help with looping problem - WHILE statement


Darkmatter5

Recommended Posts

Here's my code

 

<?php
  if(isset($_POST['LOADclient_job'])) {
  include 'library/dbconfig.php';
  include 'library/opendb.php';
                
  $query="SELECT clients.last_name, clients.first_name, clients.company_name, jobs.job_number, jobs.job_desc
    FROM byrnjobdb.clients
    INNER JOIN byrnjobdb.jobs
    ON byrnjobdb.clients.client_id = byrnjobdb.jobs.client_id
    WHERE byrnjobdb.clients.client_id = $client
    ORDER BY jobs.job_number ASC";
  $result=mysql_query($query);
  $row=mysql_fetch_array($result);
  if(empty($row['company_name'])) {
    $fullname=$row['last_name']. ", " .$row['first_name'];
  } else {
    $fullname=$row['last_name']. ", " .$row['first_name']. " of " .$row['company_name'];
  }
  echo "<table width='650' border='0'>";
  echo "<tr>";
  echo "<td colspan='2' class='row_title' align='left'>Jobs associated with '" .$fullname. "' as the client.";
  echo "</tr>";
  echo "<tr>";
  echo "<td colspan='2' class='row_description' align='left'>";
  echo "<ol>";
  while($row) {
    echo "<li>" .$row['job_number']. ", " .$row['job_desc']. "</li>";
  }
  echo "</ol></td></tr></table>";
                
  include 'library/closedb.php';
  }
?>

 

The record that I'm using to search should have 2 results, so the output should be something like.

 

1. 22222, something

2. 33333, nothing

 

But the while statement keeps running and doesn't stop. Any ideas why?

try this:

<?php
  if(isset($_POST['LOADclient_job'])) {
  include 'library/dbconfig.php';
  include 'library/opendb.php';
                
  $query="SELECT clients.last_name, clients.first_name, clients.company_name, jobs.job_number, jobs.job_desc
    FROM byrnjobdb.clients
    INNER JOIN byrnjobdb.jobs
    ON byrnjobdb.clients.client_id = byrnjobdb.jobs.client_id
    WHERE byrnjobdb.clients.client_id = $client
    ORDER BY jobs.job_number ASC";
  $result=mysql_query($query);
  $row=mysql_fetch_array($result);
  if(empty($row['company_name'])) {
    $fullname=$row['last_name']. ", " .$row['first_name'];
  } else {
    $fullname=$row['last_name']. ", " .$row['first_name']. " of " .$row['company_name'];
  }
  echo "<table width='650' border='0'>";
  echo "<tr>";
  echo "<td colspan='2' class='row_title' align='left'>Jobs associated with '" .$fullname. "' as the client.";
  echo "</tr>";
  echo "<tr>";
  echo "<td colspan='2' class='row_description' align='left'>";
  echo "<ol>";
  while($row = mysql_fetch_assoc($result)) {
    echo "<li>" .$row['job_number']. ", " .$row['job_desc']. "</li>";
  }
  echo "</ol></td></tr></table>";
                
  include 'library/closedb.php';
  }
?>

It partially works. Only one of the results show up. I've noticed that if I run a mysql_fetch_array or _assoc function on $result more then once, I only get one result. If say I get ride of this section of the code.

 

$row=mysql_fetch_array($result);
if(empty($row['company_name'])) {
  $fullname=$row['last_name']. ", " .$row['first_name'];
} else {
  $fullname=$row['last_name']. ", " .$row['first_name']. " of " .$row['company_name'];
}

 

More importantly getting rid of $row=mysql_fetch_array($result). The lower "fetch" code runs correctly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.