Jump to content


Photo

Displaying all records MySQL/PHP


  • Please log in to reply
8 replies to this topic

#1 titangf

titangf
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationColorado, USA

Posted 28 March 2006 - 09:29 PM

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.
[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--][a href="http://www.csszengarden.com/" target="_blank"]css zen garden - beautiful design with the programmer in mind[/a][!--colorc--][/span][!--/colorc--]

[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--]Time is the invention of man to gauge what his achievements have been before he fades to dust.[!--colorc--][/span][!--/colorc--]

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 29 March 2006 - 05:20 AM

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.

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 March 2006 - 09:41 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 titangf

titangf
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationColorado, USA

Posted 29 March 2006 - 04:54 PM

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.


<?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\"?".">"; ?>


and skipping down to the record recalls...


  <?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)); ?>

Would it help to do this in a For Each statement verses the While statment?
[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--][a href="http://www.csszengarden.com/" target="_blank"]css zen garden - beautiful design with the programmer in mind[/a][!--colorc--][/span][!--/colorc--]

[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--]Time is the invention of man to gauge what his achievements have been before he fades to dust.[!--colorc--][/span][!--/colorc--]

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 March 2006 - 07:18 PM

Indeed that's the problem. That's an interesting construct -- why not use the more common:

while( $row_property = mysql_fetch_assoc($property) ) {
  # your inner loop
}

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 29 March 2006 - 08:49 PM

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.

#7 titangf

titangf
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationColorado, USA

Posted 29 March 2006 - 08:58 PM

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

<?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 }?>

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.
[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--][a href="http://www.csszengarden.com/" target="_blank"]css zen garden - beautiful design with the programmer in mind[/a][!--colorc--][/span][!--/colorc--]

[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--]Time is the invention of man to gauge what his achievements have been before he fades to dust.[!--colorc--][/span][!--/colorc--]

#8 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 29 March 2006 - 08:58 PM

Did you remove the mysql_fetch_assoc() that you were doing before the loop?

#9 titangf

titangf
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationColorado, USA

Posted 29 March 2006 - 09:07 PM

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

Lesson to all to check and Double 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.

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

<?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 }?>


[!--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.
[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--][a href="http://www.csszengarden.com/" target="_blank"]css zen garden - beautiful design with the programmer in mind[/a][!--colorc--][/span][!--/colorc--]

[!--coloro:#3366FF--][span style="color:#3366FF"][!--/coloro--]Time is the invention of man to gauge what his achievements have been before he fades to dust.[!--colorc--][/span][!--/colorc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users