Jump to content

Archived

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

manmanman

PHP code returns blank

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I have another slight hitch: This code only returns one result. I clearly have two entries, but it only returns one.

Share this post


Link to post
Share on other sites
<?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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
In PHP user document it is written tht we can use either

mysql_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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]
<?php
mysql_fetch_array($result, MYSQL_ASSOC);
?>
[/code]

short way
[code]
<?php
mysql_fetch_assoc($result);
?>
[/code]

Share this post


Link to post
Share on other sites
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.

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.