Jump to content

Funny thing


psuplat

Recommended Posts

Wonder if any of you had this before.

 

I'm pulling data from db for given day, and display them in a table. After, as extra feature, I'm running a quick query checking how many record are in the db for given day and dispaying result.

 

The count works correctly - it shows the actual number of records in db, but the first part of the code - the listing of record - always skips the first record.

 

So the count returns i.e. 7 record, but on the listing shows only 6.

 

Here's the listing part of the code. Also you might notice a bit "messy" use of odbc_fetch_row and odbc_fetch_array, but that had to do with controlling situation when there was no records in db - I'll clean it up later ;)

 

Any ideas or solutions welcomed

 

echo "<table border=0 class=\"report-font-table\"><tr bgcolor=#CCCCCC><td><b>MODEL</b></td><td><b>SERIAL NUMBER</b></td><td><b>INSPECTOR</b></td><td><b>COMMENTS/FAILS</b></td></tr>";
$MySQL1 = 'select Model, Serial_no, Inspector, Comment from CM_Audit where Date=#'.$new_date.'#';
$MyCon=odbc_connect('SQA_Typewriter','','') ;         // use the SQA_Typewriter ODBC
$result=odbc_exec($MyCon,$MySQL1);

$check1=odbc_fetch_array($result);

if (!empty($check1)) {
   while (odbc_fetch_row($result)) {
    echo "<tr>
                <td>".odbc_result($result,"Model")."</td>
                <td >".odbc_result($result,"Serial_no")."</td>
                    <td >".odbc_result($result,"Inspector")."</td>
                        <td >".odbc_result($result,"Comment")."</td>
            </tr>";
  }
  echo "</table>";

      odbc_close($MyCon);
} else {
    echo "</table><p style=font-weight:bold;color:006699>No audits have been carried out on this day.</p>";
}

Link to comment
Share on other sites

Uhm,

 

i had problems with page when no record were returned from db, the page was crashing. So i tried few way to go around it, and first option that actually worked for me was using php's empty function, checking if returned array is empty.

If not list, if empty echo text.

 

You'll notice this in the code:

$check1=odbc_fetch_array($result);

if (!empty($check1)) {
   //list the records
  }
.
.
.
else {
    echo "</table><p style=font-weight:bold;color:006699>No audits have been carried out on this day.</p>";
}

 

Now if this is causing the first record to dissapear, do you know other way in which I can control returning of 0 records?

Link to comment
Share on other sites

odbc_num_rows() may work, depending on if the db driver you are using supports it.

 

odbc_fetch_array() and odbc_fetch_row() optionally take a second parameter than can be used to reset back to row 1, again depending on if the db driver you are using supports it.

 

You could also use most of your existing logic, but change the while(){} loop into a do{}while() loop so that you can use the first row that you are fetching as part of the test to see if there are any rows.

 

Also, you could just use your existing while(){} loop. Your existing code should produce an empty table when there are zero matching rows. Removing the extra fetch_array() won't change that. What sort of problems where you having that caused you to put in that extra fetch_array()?

 

Link to comment
Share on other sites

if no records were returned from db i was getting the following:

 

Warning: odbc_fetch_row(): 2 is not a valid QDBC result resource in report.php on line 48

Warning: odbc_close(): 1 is not a valid ODBC-Link resource in report.plip on line 62

 

and those 2 line where repeating in the loop causing the browser to crash

Link to comment
Share on other sites

strangely enough that was the most obvious way of doing it, but it still didn't work  :shrug:

 

but i managed to go around the problem. original structure of report was:

1. code of audit

2. echo result

3. code to count audits using select count(*)

4. echo number

 

all i did was:

1. code to count audits using select count(*)

2. store result if variable $cma

3. code of audit

4. if ($cma != 0)

5. echo results

6. else echo "no records"

7. echo $cma

 

i guess not the perfect or most optimized way of doing it, but i got no idea what odbc drivers are on the server, so I just have to wing my way around it  8)

Link to comment
Share on other sites

if no records were returned from db i was getting the following:

 

Warning: odbc_fetch_row(): 2 is not a valid QDBC result resource in report.php on line 48

Warning: odbc_close(): 1 is not a valid ODBC-Link resource in report.plip on line 62

 

and those 2 line where repeating in the loop causing the browser to crash

 

No, you would not have. The first error means that your query failed due to an error (zero matching rows is not an error), probably due to not having a valid connection to your database, which is what the second error supports as well.

 

That you have this code inside of an outer loop that you did not bother to post and that the code you did post is closing the connection explains the errors.

 

If you really want help with what your code is doing, you would need to post all the actual relevant code. You are writing and rewriting unnecessary code to compensate for some crap code  earlier in your script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.