Da Foxx Posted July 18, 2009 Share Posted July 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/ Share on other sites More sharing options...
The Little Guy Posted July 18, 2009 Share Posted July 18, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877717 Share on other sites More sharing options...
Da Foxx Posted July 18, 2009 Author Share Posted July 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877721 Share on other sites More sharing options...
The Little Guy Posted July 18, 2009 Share Posted July 18, 2009 Like this then: $i = 0; while ($row = $database->fetch_array($result_set)) { $rows[$i] = $row[0]; $i++; } return $rows; Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877725 Share on other sites More sharing options...
Da Foxx Posted July 18, 2009 Author Share Posted July 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877728 Share on other sites More sharing options...
The Little Guy Posted July 18, 2009 Share Posted July 18, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877731 Share on other sites More sharing options...
Da Foxx Posted July 18, 2009 Author Share Posted July 18, 2009 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. 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? Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877733 Share on other sites More sharing options...
GingerRobot Posted July 18, 2009 Share Posted July 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877743 Share on other sites More sharing options...
Da Foxx Posted July 18, 2009 Author Share Posted July 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877746 Share on other sites More sharing options...
GingerRobot Posted July 18, 2009 Share Posted July 18, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877751 Share on other sites More sharing options...
Da Foxx Posted July 18, 2009 Author Share Posted July 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166445-solved-retriving-mysql-table-fields/#findComment-877767 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.