Jump to content

PHP code returns blank


manmanman

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?
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/
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
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90228
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
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90858
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]
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90896
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.
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90898
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.
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90906
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.
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90925
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]
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-90959
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.
Link to comment
https://forums.phpfreaks.com/topic/20474-php-code-returns-blank/#findComment-91488
Share on other sites

Archived

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

×
×
  • Create New...

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.