Jump to content

Archived

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

titangf

Displaying all records MySQL/PHP

Recommended Posts

I have built a database that has 5 records in it and I have successfully been able to spit that information back out into a dynamic table with one exception. Here's the problem...

When the data is displayed on the page, I am missing the content from the first record and the space where the information should be filled in is instead blank.

Any ideas as to why this is happening? How do I fix it?

oh, if you should need the code I don't mind posting it... But for some reason i'm thinking this is an easy problem that i'm overlooking.

Share this post


Link to post
Share on other sites
Sounds like a simple error, but there are millions of ways to mess up. Post some code and we'll see if we can find any problems.

Share this post


Link to post
Share on other sites
Agreed -- just a shot in the dark, but leaving off the first record of a result set is usually the result (no pun intended) of inadvertently incrementing the record set pointer outside the while loop.

Share this post


Link to post
Share on other sites
Actually I'm sure that fenway is right on the money with this one. I did set the loop outside of the first record. I'm posting my code that pertains to just the PHP/MySQL work.


[code]<?php require_once('connection/data_connect.php'); ?>
<?php
mysql_select_db(ME_test);
$query_ME_test = "SELECT P_ID, P_address, P_type, P_price FROM property ORDER BY P_ID='$P_ID'";
//testing connection to see if it can see the database and code that I can omit later on
if (!mysql_select_db(ME_test)) {
die('Could not select database');
}
echo 'Connection successful!';
$property = mysql_query($query_ME_test) or die(mysql_error());
$row_proptery = mysql_fetch_assoc($property);
$totalRows_property = mysql_num_rows($property);
?>
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>[/code]


and skipping down to the record recalls...


[code]  <?php do { ?>
  <tr class="td">
    <td><?php echo $row_property['P_ID']; ?></td>
    <td><?php echo $row_property['P_address']; ?></td>
    <td><?php echo $row_property['P_type']; ?></td>
    <td>$ <?php echo number_format($row_property['P_price'],0); ?></td>
  </tr>
  <?php } while ($row_property = mysql_fetch_assoc($property)); ?>[/code]

Would it help to do this in a For Each statement verses the While statment?

Share this post


Link to post
Share on other sites
Indeed that's the problem. That's an interesting construct -- why not use the more common:

[code]while( $row_property = mysql_fetch_assoc($property) ) {
  # your inner loop
}[/code]

Share this post


Link to post
Share on other sites
Your code would work except that you misspelled $row_property as $row_proptery.

I do agree with fenway though, the do { } while (); structure you are using is unnecessary and cumbersome.

Share this post


Link to post
Share on other sites
I'm going to blame late night coding with caffeine. Yes Fenway.... this works as well... But I was in the right ballpark, lol.

[code]<?php while ($row_property = mysql_fetch_assoc($property)) {?>
  <tr class="td">
    <td><?php echo $row_property['P_ID']; ?></td>
    <td><?php echo $row_property['P_address']; ?></td>
    <td><?php echo $row_property['P_type']; ?></td>
    <td>$ <?php echo number_format($row_property['P_price'],0); ?></td>
  </tr>
<?php }?>[/code]

Now, instead of showing an empty row for the first record, it omits it completely from the list. I believe that this problem has to deal more directly with the database. For some reason I'm getting the funny feeling that it is omitting it because it is the first entry of the database (I don't know why... just a feeling). I promise you that there are a total of 6 records (I added on recently for testing purposes)

you can see for yourself (please bear in mind... this is just for playing purposes so its not pretty... just functional/disfunctional)
[a href=\"http://www.tornadopixel.com/MountainEast/\" target=\"_blank\"]http://www.tornadopixel.com/MountainEast/[/a]

lol, well I missed that one... Thanks for the spell check.

Lesson to all to check your spellings of your variables.


Unfortunately... This doesn't solve the problem I was refering to... It still does not want to display the first record. I know I can duplicate the record to make it show up, but I don't understand why its not showing.

Share this post


Link to post
Share on other sites
lol, well I missed that one... Thanks for the [i]spell check[/i].

[b]Lesson to all to check and [i]Double Check[/i] your spellings of your variables.[/b]


Unfortunately... This doesn't solve the problem I was refering to... It still does not want to display the first record. I know I can duplicate the record to make it show up, but I don't understand why its not showing.

Wickning1, Thanks. That was it...
Thanks to you both Fenway and Wickning1, you've both been a great help. Its all working now.
Final Code as it stands

[code]<?php require_once('connection/data_connect.php'); ?>
<?php
mysql_select_db(ME_test);
$query_ME_test = "SELECT P_ID, P_address, P_type, P_price FROM property ORDER BY P_ID='$P_ID'";
//testing connection to see if it can see the database
if (!mysql_select_db(ME_test)) {
die('Could not select database');
}
echo 'Connection successful!';
$property = mysql_query($query_ME_test) or die(mysql_error());
$row_property = ($property);
$totalRows_property = mysql_num_rows($property);
?>
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

//skip to call of records

  <?php while ($row_property = mysql_fetch_assoc($property)) {?>
  <tr class="td">
    <td><?php echo $row_property['P_ID']; ?></td>
    <td><?php echo $row_property['P_address']; ?></td>
    <td><?php echo $row_property['P_type']; ?></td>
    <td>$ <?php echo number_format($row_property['P_price'],0); ?></td>
  </tr>
<?php }?>[/code]


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Did you remove the mysql_fetch_assoc() that you were doing before the loop?[/quote]

By getting rid of the mysql_fetch_assoc() before the loop, I now don't have that hick-up that skips the first record. Thanks again to you both.

Share this post


Link to post
Share on other sites

×

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.