black.horizons Posted June 24, 2006 Share Posted June 24, 2006 [code] <?php $topic_query="SELECT * FROM topics ORDER BY ID"; /* Error occurred, return given name by default */ $result=mysql_query($topic_query); $num_rows=mysql_numrows($result);mysql_close(); if(!$topic_query || ($num_rows < 0)){ echo "Error displaying info"; return; } if($num_rows == 0){ echo "Database table empty"; return; } /* Display table contents */ $i=0; while ($i < $num_rows) { $id=mysql_result($result,$i,"ID"); $topic_name=mysql_result($result,$i,"TopicName"); if($i % 2) { echo "<tr class=\"tabletext\">"; } else { echo "<tr class=\"tabletextwhite\">"; } $i++; }?>[/code]its killing me. its only displaying the last record. and this is the second query i've had like this. the first i got rid of...because of the hassle. if i get this sorted i'll apply the same technique.there should be "test1", "test2" and "test3" showing, where only "test3" is showing...any help!?![a href=\"http://www.catalyticstudios.haisoft.net/annadale/forum/index2.php\" target=\"_blank\"]http://www.catalyticstudios.haisoft.net/an...orum/index2.php[/a]thats the page! Quote Link to comment https://forums.phpfreaks.com/topic/12828-ive-searched-with-no-luck-first-record-not-displaying/ Share on other sites More sharing options...
jfee1212 Posted June 24, 2006 Share Posted June 24, 2006 instead of having:[code] /* Display table contents */ $i=0; while ($i < $num_rows) { $id=mysql_result($result,$i,"ID"); $topic_name=mysql_result($result,$i,"TopicName"); if($i % 2) { echo "<tr class=\"tabletext\">"; } else { echo "<tr class=\"tabletextwhite\">"; } $i++; }[/code]try:[code]/*Display table contents */while($data_array = mysql_fetch_array($result, MYSQL_ASSOC)){$id = $data_array['ID'];$topic_name = $data_array['TopicName']; if($i % 2) { echo "<tr class=\"tabletext\">"; } else { echo "<tr class=\"tabletextwhite\">"; } echo $topic_name . "</tr>"; $i++;}[/code]I don't see where you echo $topic_name, perhaps on another script. Wherever you did, delete it, and this will print the correct variables.The MYSQL_ASSOC creates an associative array, which can be accessed using $array_name['fieldname'] In this case I named the array $data_array, but it can be called anything.Each time it passes through the while loop, it takes the next row and uses that as the row to choose fields from.Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/12828-ive-searched-with-no-luck-first-record-not-displaying/#findComment-49203 Share on other sites More sharing options...
Barand Posted June 24, 2006 Share Posted June 24, 2006 I'm amazed it's even displaying the last record. All the posted code does is echo <tr> tags.Try[code] $topic_query="SELECT ID, topicName FROM topics ORDER BY ID"; $result=mysql_query($topic_query); if(!$result){ echo "Error displaying info"; return; } $num_rows=mysql_num_rows($result); if($num_rows == 0){ echo "Database table empty"; return; } /* Display table contents */ $i=0; echo '<table>'; while (list($id, $topic_name)=mysql_fetch_row($result)) { if($i++ % 2) { echo "<tr class=\"tabletext\">"; } else { echo "<tr class=\"tabletextwhite\">"; } echo "<td>$id</td><td>$topic_name</td></tr>"; } echo '</table>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12828-ive-searched-with-no-luck-first-record-not-displaying/#findComment-49205 Share on other sites More sharing options...
jfee1212 Posted June 25, 2006 Share Posted June 25, 2006 Is the updated script uploaded to your site? Quote Link to comment https://forums.phpfreaks.com/topic/12828-ive-searched-with-no-luck-first-record-not-displaying/#findComment-49220 Share on other sites More sharing options...
black.horizons Posted August 15, 2006 Author Share Posted August 15, 2006 fortunately it now works. i spent ages working on other things, and got it to work. thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/12828-ive-searched-with-no-luck-first-record-not-displaying/#findComment-75078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.