RopeADope Posted November 5, 2010 Share Posted November 5, 2010 Hi all. The following is a function to get and display data. However, I've hit a wall in my progress. It's only printing out one field. I need to have it print out every retrieved field. Any help is appreciated. function get_data($table_name){ $result=mysql_query("SELECT * FROM $table_name"); $num=mysql_num_rows($result); $i=0; while($i < $num){ $fields=mysql_fetch_field($result,$i); $display_fields=mysql_result($result,$i,$fields->name); echo $display_fields; $i++; }; }; I realized that (I think) I need the $display_fields var to be concatenated with the $i variable so there's a unique var name for each field retrieved from the mysql_fetch_field line. Any ideas? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 5, 2010 Share Posted November 5, 2010 Depends on what the field names are and how you want them displayed, but here is the standard way of doing this: $result = mysql_query("SELECT * FROM $table_name"); while($row = mysql_fetch_object($result)) { //or mysql_fetch_assoc() echo $row->name; //or $row['name'] } Quote Link to comment Share on other sites More sharing options...
RopeADope Posted November 5, 2010 Author Share Posted November 5, 2010 I can get the field names okay. I can't seem to get the while() to print out all the records of the given argument. function get_data($table_name){ //Query $result=mysql_query("SELECT * FROM $table_name"); //Count records $num=mysql_num_rows($result); $i=0; while($i < $num){ //Get field names $fields=mysql_fetch_field($result,$i); //Trying to do: Put query results recursively into a $var $display_fields=mysql_result($result,$i,$fields->name); //Display data echo $display_fields; $i++; }; }; My Problem: I can't figure out how to add a count to the $display_fields so I get a unique $var at every iteration. Right now, this function only prints out a single record. I tried to do $display_fields.$i=mysql_result($result,$i,$fields->name); but to no avail... Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 5, 2010 Share Posted November 5, 2010 Instead of reposting your code, what does my code not do that you need it to do? Quote Link to comment Share on other sites More sharing options...
RopeADope Posted November 5, 2010 Author Share Posted November 5, 2010 Instead of reposting your code, what does my code not do that you need it to do? I inserted your code, but nothing gets displayed at all. I just tried... function get_data($table_name){ /* $result=mysql_query("SELECT * FROM $table_name"); $num=mysql_num_rows($result); $i=0; while($i < $num){ $fields=mysql_fetch_field($result,$i); $display_fields=mysql_result($result,$i,"$fields->name"); echo " " . $display_fields . "<br />"; $i++; }; */ $result = mysql_query("SELECT * FROM $table_name"); while($row = mysql_fetch_object($result)) { //or mysql_fetch_assoc() echo $row->name; //or $row['name'] }; }; Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 5, 2010 Share Posted November 5, 2010 Is there a column "name" in that table? Give an example of what output you want. Quote Link to comment Share on other sites More sharing options...
RopeADope Posted November 9, 2010 Author Share Posted November 9, 2010 Is there a column "name" in that table? Give an example of what output you want. Every table has different column names. I don't believe any have a column of "name". In terms of output, I'm just looking to list all the data in a table as ordered rows. Ex: ID client date X Y Z 1 Home 2/2/10 1 2 3 2 Business 1/2/10 4 5 6 etc... Quote Link to comment Share on other sites More sharing options...
DavidAM Posted November 9, 2010 Share Posted November 9, 2010 You need another loop to walk through each field in each row. While this is not the way that I would do it, the following code adds this loop similar to the style you were using in your original post. You'll probably want to wrap TABLE tags (and TR / TD) around the output, since I did not put in any spacing (except line breaks). * This code is NOT tested function get_data($table_name){ //Query $result=mysql_query("SELECT * FROM $table_name"); // Output the field names $fldCnt = mysql_num_fields($result); for ($j = 0; $j < $fldCnt; $j++) { $field = mysql_fetch_field($result, $j); echo $field->name; } echo '<BR>'; //Count records $num=mysql_num_rows($result); $i=0; while($i < $num){ // Loop through each field in the result row for ($j = 0; $j < $fldCnt; $j++) { $data = mysql_result($result, $i, $j); echo $data; } echo '<BR>'; $i++; }; }; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.