Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/2768-displaying-the-result-set/
Share on other sites

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'];
   ......
}

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!

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!

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.

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   or tables. Also, you could surround the output with <pre>.

 

feel free to PM me if you need more help :)

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

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!

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.