manmanman Posted September 12, 2006 Share Posted September 12, 2006 Does anyone know why this code:[code]echo "<table>"while($result = mysql_fetch_array($sql)) { echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; }echo "</table>"[/code]returns a blank? If I remove the while, it works, but I want it executed multiple times if possible. How would I resolve this? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted September 12, 2006 Share Posted September 12, 2006 did you already connect to the database? Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted September 12, 2006 Share Posted September 12, 2006 try this:[code]<?php$query="SELECT * FROM `table` WHERE `column` = 'value' "; // Change to your own MySQL code$query_results=mysql_query($query);while($result=mysql_fetch_assoc($query_results)){ echo "<table>"; echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>"; }?>[/code]just connect to the DB. and replace the sql query from above. ot your own and it should work. Hope This Helps,Brenden Quote Link to comment Share on other sites More sharing options...
manmanman Posted September 12, 2006 Author Share Posted September 12, 2006 Yay! It works! Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 12, 2006 Share Posted September 12, 2006 echo your query to see that it is executing what you want it to.Then change your mysql_connect/query/select_db function calls to suffix 'or die(mysql_error());' on the end, like so:[code]<?php$result = mysql_query($query) or die(mysql_error());?>[/code] Quote Link to comment Share on other sites More sharing options...
manmanman Posted September 13, 2006 Author Share Posted September 13, 2006 I have another slight hitch: This code only returns one result. I clearly have two entries, but it only returns one. Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted September 13, 2006 Share Posted September 13, 2006 <?php$query="SELECT * FROM `table` WHERE `column` = 'value' "; // Change to your own MySQL code$query_results=mysql_query($query);while($result=mysql_fetch_array($query_results)){ echo "<table>"; echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>"; }?>try that.. the only thing i changed was.. "assoc" to "array" in mysql_fetch_array Quote Link to comment Share on other sites More sharing options...
manmanman Posted September 13, 2006 Author Share Posted September 13, 2006 Still the same :( Quote Link to comment Share on other sites More sharing options...
Ravi Kumar Posted September 13, 2006 Share Posted September 13, 2006 try the following.It should work.[code]<?php$query="SELECT * FROM `table` WHERE `column` = 'value' "; // Change to your own MySQL code$query_results=mysql_query($query);while($result=mysql_fetch_array($query_results),MYSQL_NUM){ echo "<table>"; echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 run the query manually in mysql command prompt/phpmyadmin/mysql administrator to see expected results.Ravi - no, that will not work. You have specified for mysql_fetch_array to return a numerically indexed array, but then go on to use associative index's..mysql_fetch_assoc() will do fine, changing to *_array was unecessary. Quote Link to comment Share on other sites More sharing options...
Ravi Kumar Posted September 13, 2006 Share Posted September 13, 2006 sorry for the copy-paste error.[code]<?php$query="SELECT * FROM `table` WHERE `column` = 'value' "; // Change to your own MySQL code$query_results=mysql_query($query);while($result=mysql_fetch_array($query_results,MYSQL_NUM)){ echo "<table>"; echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>"; }?>[/code]the difference of my previous post and this post is "MYSQL_NUM" in inside mysql_fectch_array().This is working for me!.I am using this in my application. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 Ravi - no, that will not work. You have specified for mysql_fetch_array to return a numerically indexed array, but then go on to use associative index's..still applies to your new example. Quote Link to comment Share on other sites More sharing options...
Ravi Kumar Posted September 13, 2006 Share Posted September 13, 2006 In PHP user document it is written tht we can use eithermysql_fetch_array($query_results,MYSQL_NUM);or mysql_fectch_array($query_results,MYSQL_ASSOC);in this case if we want to write code like[code] echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>";}[/code]we need to use while($result=mysql_fetch_array($query_results,MYSQL_ASSOC)).This is working fine in my application. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 Correct, then why did you post MYSQL_NUM in your examples?Also worth noting that[code=php:0]mysql_fetch_assoc($result);[/code] achieves the same functionality as [code=php:0]mysql_fetch_array($result, MYSQL_ASSOC);[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 13, 2006 Share Posted September 13, 2006 jenk was trying to show you the easy way as you can see from jenk last post.the whole idear of computer programming is to not write a lot but as little to get a code to work.please take as much notice as possable to members posts as your benifit from it ok.good luck.example long way for assoc in fact your turning an array into a assoc so the short example is a lot faster and smarter way to code.[code]<?phpmysql_fetch_array($result, MYSQL_ASSOC);?>[/code]short way[code]<?phpmysql_fetch_assoc($result);?>[/code] Quote Link to comment Share on other sites More sharing options...
manmanman Posted September 14, 2006 Author Share Posted September 14, 2006 I just realised something:[code=php:0]while($result = mysql_fetch_array($result)) { echo "<table border='1'>"; echo "<tr>"; echo "<th>Character Name:</th> <td>".$result['name'] . "</td></tr>"; echo "<th>HP:</th> <td>".$result['mp'] . " </td></tr>"; echo "<th>MP:</th> <td>" . $result['mp'] . " </td></tr>"; echo "<th>EXP:</th> <td>" . $result['exp'] . " </td></tr>"; echo "<th>SP:</th> <td>" . $result['sp'] . " </td></tr>"; echo "</table>"; }[/code]was my original code. I realised this: [code=php:0]while($result = mysql_fetch_array($result)) {[/code]I changed it to: [code=php:0]while($get_result = mysql_fetch_array($result)) {[/code]And now it works. Quote Link to comment 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.