Jump to content

Get/display data


RopeADope

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/#findComment-1130714
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/#findComment-1130726
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/#findComment-1130737
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/#findComment-1132273
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/217854-getdisplay-data/#findComment-1132289
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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