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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.