Jump to content

Recommended Posts

I've been at this for sometime now, and can't get it to work the way I want it to. Here is my problem:

 

Instead of having to always update my array with new table fields, example:

protected static $table_name_fields = array('id', 'username', 'password');

 

I wanted to create a method inside the class that would retrieve all table fields and put them into an array as well as assign them as public variable below. This is what I have so far:

	protected static $table_name = "members";

	public static function output_fields() {
		global $database;
		$sql = "SHOW FIELDS FROM ".self::$table_name;
		$result_set = $database->query($sql);
		while ($row = $database->fetch_array($result_set)) {
			$row[0];
		}
		return $row;

	}

	// I want PHP to handle this by it self:
                protected static $table_name_fields = array('id', 'username', 'password');

	public $id;
	public $username;
	public $password;

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/
Share on other sites

try changing this:

while ($row = $database->fetch_array($result_set)) {
$row[0];
}
return $row;

 

to this:

while ($row = $database->fetch_array($result_set)) {
$rows = $row[];
}
return $rows;

 

I would get this error: Fatal error: Cannot use [] for reading in C:\wamp\www\project\includes\user.php on line 14

Line 14:

$rows = $row[];

 

And if I replace:

protected static $table_name_fields = array('id', 'username', 'password');

with:

protected static $table_name_fields = array(self::output_fields());

of course that won't work, but you should get what effect I want to create.

		public static function output_fields() {
		global $database;
		$sql = "SHOW FIELDS FROM ".self::$table_name;
		$result_set = $database->query($sql);
		while ($row = $database->fetch_array($result_set)) {
			echo "'{$row[0]}', ";
		}
		return $row;

	}

That works fine, and I have no problem with it.

 

If I were to put, let say this into index.php:

<?php echo User::output_fields(); ?>

It would output all the table fields. My problem is how would I use that to put that into an array?

 

Because the project I am working on, the database gets big and I delete and add new stuff all the time, but don't want to keep editing the array.

change these two:

echo "'{$row[0]}', ";
return $row;

 

to these:

array_push($rows, $row[0]);
return $rows;

 

Then above your while loop put this in:

$rows = array();

 

Next in your index.php replace this:

<?php echo User::output_fields(); ?>

 

With this:

<?php
$opt = User::output_fields();
print_r($opt);
?>

change these two:

echo "'{$row[0]}', ";
return $row;

 

to these:

array_push($rows, $row[0]);
return $rows;

 

Then above your while loop put this in:

$rows = array();

 

Next in your index.php replace this:

<?php echo User::output_fields(); ?>

 

With this:

<?php
$opt = User::output_fields();
print_r($opt);
?>

Thanks for the help, but I don't think your reading me right.  :shrug:

This is just a modified example:

	public static function output_fields() {
		global $database;
		$sql = "SHOW FIELDS FROM ".self::$table_name;
		$result_set = $database->query($sql);
		while ($row = $database->fetch_array($result_set)) {
			echo "'{$row[0]}', ";
		}
		return $row;

	}

I want to have the fields that output from the table and placed into an array. What I used was just an example. When I run this statement:

<?php echo User::output_fields(); ?>

it outputs on the front page:

'id', 'ip', 'username', 'password', 'rank',

 

How can I use that echo to "echo" inside of an array so that when PHP reads it, it reads it like an array with values?

As I understand your question, The Little Guy answered it correctly. So if you're not actually wanting the function to return an array containing all the field names, then you're going to have to re-explain yourself.

I knew I didn't make myself clear enough. I apologize. I don't want it to output anything. His methods worked fine.

	protected static $table_name_fields = array('id', 'username', 'password', 'rank'); // This is predefined array with all the MySQL fields from a selected table. Of course the list grows as I add new fields, but I don't want to manually have to add the fields into the array. I want a function that can handle that for me.

                // I also want a function that can add a new public variable for each of value in the array corrisponding to its value as you see here:
	public $id;
	public $username;
	public $password;
	public $rank;

 

How would I do that? I hope I made myself more clear, thanks for you help guys!

Well you've been shown how to do the first part...though you might wish to have the constructor of your class call the function if you don't wish to call it explicitly each time:

 

class foo{
    private $fields = array();
    function __construct(){
        echo "html {$this->getVar()}" . "\n";
        $this->setFields();
    }

    private function setFields() {
        global $database;
        $sql = "SHOW FIELDS FROM ".self::$table_name;
        $result_set = $database->query($sql);
        while ($row = $database->fetch_array($result_set)) {
            $this->fields[] = $row[0];    
        }
    }
}

 

Obviously that method wont work if you're really wanting a public static function. But do you?

 

As for question two, why would you want to do this?

Well you've been shown how to do the first part...though you might wish to have the constructor of your class call the function if you don't wish to call it explicitly each time:

 

class foo{
    private $fields = array();
    function __construct(){
        echo "html {$this->getVar()}" . "\n";
        $this->setFields();
    }

    private function setFields() {
        global $database;
        $sql = "SHOW FIELDS FROM ".self::$table_name;
        $result_set = $database->query($sql);
        while ($row = $database->fetch_array($result_set)) {
            $this->fields[] = $row[0];    
        }
    }
}

 

Obviously that method wont work if you're really wanting a public static function. But do you?

 

As for question two, why would you want to do this?

 

I'm still pretty new to object oriented programing, but this was done so that I could avoid get_object_vars and wanted to create a more dynamic array that could use the MySQL SHOW ALL FIELDS command and have it list all the fields for me without having to manually put them in there.

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.