FirstBorn Posted December 31, 2008 Share Posted December 31, 2008 Hi, Thank you for reading. I have a php script that I'm using that has been used before on a different site, but now, when attempting to use the following code, it is not displaying the data that I expect for it to. <?php // connecttodb($servername,$dbname,$dbuser,$dbpassword) $query="SELECT * FROM directory WHERE active='1' AND location='L' AND type='paid' ORDER BY sortorder ASC"; $result = mysql_query ($query) or die(mysql_error()); $num=mysql_num_rows($result); // mysql_close(); $id=mysql_result($result,$i,"id"); $title=mysql_result($result,$i,"title"); $url=mysql_result($result,$i,"url"); $sortorder=mysql_result($result,$i,"sortorder"); $type=mysql_result($result,$i,"type"); $location=mysql_result($result,$i,"location"); $class=mysql_result($result,$i,"class"); $active=mysql_result($result,$i,"active"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<b>$class<br><a href=\"$url\">$title</a><hr>"; } mysql_free_result($result); mysql_close(); ?> The above displays 1 record when there should be 2. The code below displays 12 rows of the same record when there should be 12 different records displayed: <p><h3>Available</h3><hr> <?php connecttodb($servername,$dbname,$dbuser,$dbpassword); $query="SELECT * FROM directory WHERE active='1' AND location='L' AND type='order' ORDER BY sortorder ASC"; // $result=mysql_query($query); $result = mysql_query ($query) or die(mysql_error()); $num=mysql_num_rows($result); $id=mysql_result($result,$i,"id"); $title=mysql_result($result,$i,"title"); $url=mysql_result($result,$i,"url"); $sortorder=mysql_result($result,$i,"sortorder"); $type=mysql_result($result,$i,"type"); $location=mysql_result($result,$i,"location"); $class=mysql_result($result,$i,"class"); $active=mysql_result($result,$i,"active"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<b>$class<br><a href=\"$url\">$title</a><br>"; } mysql_close(); ?> So, I'm guessing that either: a. The "while" statement is in the wrong place (of which, I don't think so because this is how it is used on a different site) OR b. There is nothing to count and then display the next record, but, again, (I don't think so because this is how it is used on a different site) So, what am I missing here? Any Ideas? Thank you in advance for your help. Thanks, FirstBorn MySQL Ver: 5.0.51a-community phpMyAdmin: 2.11.9.1 MySQL client version: 4.1.22 Quote Link to comment https://forums.phpfreaks.com/topic/139023-solved-unexpected-output-using-while-row-mysql_fetch_arrayresult-mysql_assoc/ Share on other sites More sharing options...
rhodesa Posted December 31, 2008 Share Posted December 31, 2008 the code should be: <?php // connecttodb($servername,$dbname,$dbuser,$dbpassword) $query="SELECT * FROM directory WHERE active='1' AND location='L' AND type='paid' ORDER BY sortorder ASC"; $result = mysql_query ($query) or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<b>{$row['class']}<br><a href=\"{$row['url']}\">{$row['title']}</a><hr>"; } mysql_free_result($result); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/139023-solved-unexpected-output-using-while-row-mysql_fetch_arrayresult-mysql_assoc/#findComment-727104 Share on other sites More sharing options...
FirstBorn Posted December 31, 2008 Author Share Posted December 31, 2008 the code should be: <?php // connecttodb($servername,$dbname,$dbuser,$dbpassword) $query="SELECT * FROM directory WHERE active='1' AND location='L' AND type='paid' ORDER BY sortorder ASC"; $result = mysql_query ($query) or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<b>{$row['class']}<br><a href=\"{$row['url']}\">{$row['title']}</a><hr>"; } mysql_free_result($result); mysql_close(); ?> Dear Aaron, Thank you VERY MUCH!!! That worked! How do I get this Thread marked as 'resolved?' Thanks, Christopher Quote Link to comment https://forums.phpfreaks.com/topic/139023-solved-unexpected-output-using-while-row-mysql_fetch_arrayresult-mysql_assoc/#findComment-727112 Share on other sites More sharing options...
rhodesa Posted December 31, 2008 Share Posted December 31, 2008 there should be a solved button somewhere Quote Link to comment https://forums.phpfreaks.com/topic/139023-solved-unexpected-output-using-while-row-mysql_fetch_arrayresult-mysql_assoc/#findComment-727113 Share on other sites More sharing options...
FirstBorn Posted December 31, 2008 Author Share Posted December 31, 2008 there should be a solved button somewhere Hi Aaron, Found it, Thanks... Was on the bottom of the page and nearly out of sight... Have an Excellent New Year! Thanks, Christopher Quote Link to comment https://forums.phpfreaks.com/topic/139023-solved-unexpected-output-using-while-row-mysql_fetch_arrayresult-mysql_assoc/#findComment-727116 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.