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
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
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
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
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
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
Share on other sites

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.