Jump to content


Photo

Displaying the result set


  • Please log in to reply
10 replies to this topic

#1 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 31 October 2005 - 12:00 PM

Hey everyone,

This is probably a really simple newbie question but I've been reading my book on this stuff and looking through posts and cant find something similar.

The deal is I want echo the information I stored my array of the result set. I got it to work if I ask it for one field, like the name, but how can I get it to display all the fields for one item and then list the next item and its fields on a new row, etc.

The code ahead of what I'm going to post works, because it is just asking for the connection to the DB and I already tested that part.

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
//Creates a List Selection query.
$strListQuery = \"SELECT * FROM horses ORDER BY horse_name\";
//Select database
mysql_select_db($strDbName, $conMySql);
//Query the database and store the Result Set.
$rsHorseList = mysql_query($strListQuery);
//close the connection to the database
mysql_close($conMySql);

//Display the list of jokes returned.
//Get the number of rows in the horse table and store in $intNumRows
$intNumRows = mysql_num_rows($rsHorseList);
//for each row
for( $i = 0; $i <$intNumRows; $i++)
{
   //Get a row of data and store it in $arrHorses
   $arrHorses = mysql_fetch_array($rsHorseList);
   //display the record of each horse
   echo $arrHorses[\'\'];
   //close anchor tag
}
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

I know my problem is at the end with how to display it...I just dont know what else to try. I realize what I posted is an empty set and well isnt displaying anything.

Thank for any help!

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 October 2005 - 04:41 PM

if you use mysql_fetch_array, the column names as well as the column offsets are available to use. the * syntax of mysql gives you all the fields, so you just reference them the same way you would a single field.
//the following two lines echo the same field
echo $arrHorses[0];
echo $arrHorses['first_field'];

this is an alternative method of accessing the fields using a while loop. i'm not saying that your method is incorrect, but if you don't need the value from mysql_num_row, you can save some memories and process time.
while($arrHorses = mysql_fetch_array($rsHorseList))
{
    echo $arrHorses['first_field'];
    ......
}

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 31 October 2005 - 08:12 PM

Hmm..Ok, havent tried it yet. I would have thought that since I selected all (*) that there would be a way to display it all with a word or symbol instead of having to list all the field names. I believe you thats what has to be done. I just thought that would have been the purpose of selecting all of them in the first place. Codes a funny thing!

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 October 2005 - 09:22 PM

you can just use a foreach loop to loop through all the fields, just one extra line.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 01 November 2005 - 04:36 AM

I'm not good loops, one of my least favorite things, can you possibly give me an example of how it would work?

Thanks!

Side note: another weird thing is in my latest version of my document "\n" is not making a new line! Its only making a nonbreaking space...computers are weird!

#6 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 01 November 2005 - 06:36 AM

Also, besides "\n" not creating a new line, I am also not able to get "\t" to create a tab.

Anyone on any ideas how I can get tabs between the column names that I have now echoed in and then when the results are displayed a tab between each field as well? Trying to format it a lil better so it doesnt run together and so it can be read easily.

#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 November 2005 - 08:38 PM

as simple loop to echo all fields
while($arrHorses = mysql_fetch_array($rsHorseList))
{
   foreach($arrHorses as $key=>$value)
      echo $value;
   ......
}

\n and \t puts newline and tabs to the HTML documents (when you view source you see them), but when it's displayed on the browser, they're treated as spaces. to display newlines in HTML, use <br>. For tabs, you either have to go with &nbsp; or tables. Also, you could surround the output with <pre>.

feel free to PM me if you need more help :)
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 05 November 2005 - 03:54 AM

Ok, I tried to add some code that someone else suggested to display all the results and the table to make it look pretty but it doesnt display the results (no errors) - just no info on the page.... I also tried to put my column names in a table to make them pretty and they echo out but not in a table....I feel so stupid, I know HTML (altho I hate table code) but I guess since I'm new to PHP and all I dont know how to implement it well into the PHP...

help, again, please?? Thanks!!

P.S. I tried the code listed here too and got the same thing....no info on the page, no error just no info displayed...

I know I sound like a really dumb newbie..hehe

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]//Display column names
echo \'<table><br>\';
echo \'<b><td>Horse ID</td>\', \'<td>Horse Name</td>\', \'<td>Horse DOB</td>\', \'<td>Horse Sex</td>\', \'<td>Horse Height</td>\', \'<td>Horse Color</td>\', \'<td>Horse\\'s Sire</td>\', \'<td>Horse\\'s Dam</td></b><br>\';
echo \"</table>\";

//Display the list of records in the Result Set.
//Get the number of rows in the \"horses\" table and store in $intNumRows
echo \"<table>\";
$intNumRows = mysql_num_rows($rsHorseList);
for( $i = 0; $i < $intNumRows; $i++)
{
echo \"<tr>\";
$arrHorses = mysql_fetch_array($rsHorseList);
  for($j = 0; $j < 30; $j++)
  {
  echo \"<td>\".$arrHorses[j].\"</td>\";
  }
echo \"</tr>\";
}
echo \"</table>\";[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Keep in mind of course theres code above and below this..but this is the section that isnt doing anything..

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 05 November 2005 - 05:09 AM

$arrHorses[$j]
should have the dollar sign.
and it's totally ok to ask these questions :) we all went through these... sometimes a pro makes those mistakes. so it's no big deal :)
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 angel_cowgirl

angel_cowgirl
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationPoedunk America

Posted 05 November 2005 - 06:39 AM

YAY!!! It all looks pretty now...first I wasnt sure if id get the column headers to work too but their in the right way now and even bold :)

Thats so awesome, it worked for the result set part, even with the table! Thank you!! The other person had totally left off the $ for the J's and i caught it and added it in but guess i just missed one.

Thanks again so much for ur help! So appreciated!

#11 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 05 November 2005 - 06:43 AM

glad that it worked!! well just add some padding/margins with stylesheet and your table would look good.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users