Author Topic: PHP code returns blank  (Read 1437 times)

0 Members and 1 Guest are viewing this topic.

Offline manmanmanTopic starter

  • Irregular
  • Posts: 23
  • if (brain = true) { kill('brain'); } //Kills brain
    • View Profile
PHP code returns blank
« on: September 12, 2006, 01:37:23 AM »
Does anyone know why this code:
Code: [Select]
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?

Offline techiefreak05

  • Enthusiast
  • Posts: 497
  • Gender: Male
  • hmmm...
    • View Profile
    • tyny.us - Link Shortener
Re: PHP code returns blank
« Reply #1 on: September 12, 2006, 01:43:43 AM »
did you already connect to the database?
Link shortener with advanced, detailed statistics:

http://tyny.us/

Offline techiefreak05

  • Enthusiast
  • Posts: 497
  • Gender: Male
  • hmmm...
    • View Profile
    • tyny.us - Link Shortener
Re: PHP code returns blank
« Reply #2 on: September 12, 2006, 01:47:27 AM »
try this:
Code: [Select]
<?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
« Last Edit: September 12, 2006, 01:50:43 AM by techiefreak05 »
Link shortener with advanced, detailed statistics:

http://tyny.us/

Offline manmanmanTopic starter

  • Irregular
  • Posts: 23
  • if (brain = true) { kill('brain'); } //Kills brain
    • View Profile
Re: PHP code returns blank
« Reply #3 on: September 12, 2006, 04:17:23 AM »
Yay! It works!
« Last Edit: September 12, 2006, 04:18:57 AM by manmanman »

Offline Jenk

  • Devotee
  • Posts: 779
    • View Profile
Re: PHP code returns blank
« Reply #4 on: September 12, 2006, 04:20:05 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:

Code: [Select]
<?php
$result 
mysql_query($query) or die(mysql_error());
?>

Offline manmanmanTopic starter

  • Irregular
  • Posts: 23
  • if (brain = true) { kill('brain'); } //Kills brain
    • View Profile
Re: PHP code returns blank
« Reply #5 on: September 12, 2006, 11:55:18 PM »
I have another slight hitch: This code only returns one result. I clearly have two entries, but it only returns one.

Offline techiefreak05

  • Enthusiast
  • Posts: 497
  • Gender: Male
  • hmmm...
    • View Profile
    • tyny.us - Link Shortener
Re: PHP code returns blank
« Reply #6 on: September 12, 2006, 11:58:01 PM »
<?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/

Offline manmanmanTopic starter

  • Irregular
  • Posts: 23
  • if (brain = true) { kill('brain'); } //Kills brain
    • View Profile
Re: PHP code returns blank
« Reply #7 on: September 12, 2006, 11:59:41 PM »
Still the same :(

Offline Ravi Kumar

  • Irregular
  • Posts: 5
    • View Profile
Re: PHP code returns blank
« Reply #8 on: September 13, 2006, 04:47:57 AM »
try the following.It should work.

Code: [Select]
<?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>";
 }
?>



Offline Jenk

  • Devotee
  • Posts: 779
    • View Profile
Re: PHP code returns blank
« Reply #9 on: September 13, 2006, 04:50:09 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.

Offline Ravi Kumar

  • Irregular
  • Posts: 5
    • View Profile
Re: PHP code returns blank
« Reply #10 on: September 13, 2006, 04:57:07 AM »
sorry for the copy-paste error.

Code: [Select]
<?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.

Offline Jenk

  • Devotee
  • Posts: 779
    • View Profile
Re: PHP code returns blank
« Reply #11 on: September 13, 2006, 04:58:19 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.

Offline Ravi Kumar

  • Irregular
  • Posts: 5
    • View Profile
Re: PHP code returns blank
« Reply #12 on: September 13, 2006, 05:26:53 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

Code: [Select]
    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.

Offline Jenk

  • Devotee
  • Posts: 779
    • View Profile
Re: PHP code returns blank
« Reply #13 on: September 13, 2006, 06:02:05 AM »
Correct, then why did you post MYSQL_NUM in your examples?

Also worth noting thatmysql_fetch_assoc($result); achieves the same functionality as mysql_fetch_array($resultMYSQL_ASSOC);

Offline redarrow

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: PHP code returns blank
« Reply #14 on: September 13, 2006, 07:14:43 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.

Code: [Select]
<?php
mysql_fetch_array
($resultMYSQL_ASSOC);
?>


short way
Code: [Select]
<?php
mysql_fetch_assoc
($result);
?>

« Last Edit: September 13, 2006, 07:20:39 AM by redarrow »
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.ath.cx/paypal_info/paypal1_info.inc

Offline manmanmanTopic starter

  • Irregular
  • Posts: 23
  • if (brain = true) { kill('brain'); } //Kills brain
    • View Profile
Re: PHP code returns blank
« Reply #15 on: September 13, 2006, 11:55:35 PM »
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.