Jump to content


Photo

PHP code returns blank


  • Please log in to reply
15 replies to this topic

#1 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 September 2006 - 05:37 AM

Does anyone know why this 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>"
returns a blank? If I remove the while, it works, but I want it executed multiple times if possible. How would I resolve this?

#2 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 12 September 2006 - 05:43 AM

did you already connect to the database?
Link shortener with advanced, detailed statistics:

http://tyny.us/

#3 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 12 September 2006 - 05:47 AM

try this:
<?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>";
 }
?>

just connect to the DB. and replace the sql query from above. ot your own and it should work.

Hope This Helps,
Brenden
Link shortener with advanced, detailed statistics:

http://tyny.us/

#4 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 12 September 2006 - 08:17 AM

Yay! It works!

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 08:20 AM

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:

<?php
$result = mysql_query($query) or die(mysql_error());
?>


#6 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 13 September 2006 - 03:55 AM

I have another slight hitch: This code only returns one result. I clearly have two entries, but it only returns one.

#7 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 13 September 2006 - 03:58 AM

<?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 shortener with advanced, detailed statistics:

http://tyny.us/

#8 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 13 September 2006 - 03:59 AM

Still the same :(

#9 Ravi Kumar

Ravi Kumar
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 September 2006 - 08:47 AM

try the following.It should work.

<?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>";
 }
?>



#10 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 September 2006 - 08:50 AM

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.

#11 Ravi Kumar

Ravi Kumar
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 September 2006 - 08:57 AM

sorry for the copy-paste error.

<?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>";
 }
?>

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.


#12 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 September 2006 - 08:58 AM

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.

#13 Ravi Kumar

Ravi Kumar
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 September 2006 - 09:26 AM

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

    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>";
}

we need to use while($result=mysql_fetch_array($query_results,MYSQL_ASSOC)).

This is working fine in my application.

#14 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 September 2006 - 10:02 AM

Correct, then why did you post MYSQL_NUM in your examples?

Also worth noting that
mysql_fetch_assoc($result);
achieves the same functionality as
mysql_fetch_array($result, MYSQL_ASSOC);


#15 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 September 2006 - 11:14 AM

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.

<?php
mysql_fetch_array($result, MYSQL_ASSOC);
?>

short way
<?php
mysql_fetch_assoc($result);
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#16 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 14 September 2006 - 03:55 AM

I just realised something:

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>";  
    }

was my original code. I realised this:
while($result = mysql_fetch_array($result)) {

I changed it to:
while($get_result = mysql_fetch_array($result)) {

And now it works.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users