random1 Posted February 8, 2008 Share Posted February 8, 2008 How can I set variables from a database dynamically? What I mean is to create a PHP variable that is the field name in a result set. I have a language_strings table and it has 300 rows per language. I need a way of automatically creating the variables. Help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/ Share on other sites More sharing options...
resago Posted February 8, 2008 Share Posted February 8, 2008 extract() Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/#findComment-461358 Share on other sites More sharing options...
random1 Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks I've tried it with my array but the variable names confuse me. My Array is outputted as: Array ( [0] => Array ( [iD] => 1 [string Name] => login message [string Text] => Welcome, please login ... ) [1] => Array ( [iD] => 2 [string Name] => login successful message [string Text] => You have successfully logged in. ) [2] => Array ( [iD] => 3 [string Name] => login unsuccessful message [string Text] => You failed to login. Please check the username and password you enterd and the profile you selected. ) [3] => Array ( [iD] => 4 [string Name] => login heading [string Text] => Login ) [4] => Array ( [iD] => 5 [string Name] => logout link [string Text] => Logout ) [5] => Array ( [iD] => 6 [string Name] => logout successful message [string Text] => You have logged out of your account successfully. ) [6] => Array ( [iD] => 7 [string Name] => xhtml [string Text] => XHTML ) [7] => Array ( [iD] => 8 [string Name] => css2 [string Text] => CSS2 ) [8] => Array ( [iD] => 9 [string Name] => powered by [string Text] => powered by phpSandbox ) [9] => Array ( [iD] => 10 [string Name] => language [string Text] => Australian English ) [10] => Array ( [iD] => 11 [string Name] => author [string Text] => Author ) [11] => Array ( [iD] => 12 [string Name] => javascript message full [string Text] => String for Javascript I've tried: extract($results, EXTR_PREFIX_ALL, "lang"); Can someone help with using extract() on this array? Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/#findComment-461389 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 Please show us the code you use the get the array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/#findComment-461392 Share on other sites More sharing options...
random1 Posted February 8, 2008 Author Share Posted February 8, 2008 public function getRecords($sql) { $records = array(); //variables $connection = $this->m_Connection; //get the result from database with SQL $result = $connection->query($sql); if ($connection->error) { $errorMessage = '**** mysqli error ' . $connection->errno. ': ' . $connection->error; error_log($errorMessage); throw new DatabaseBadSqlStatementException($errorMessage); } //read out data while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ $records[] = $row; } return $records; } $results = $database->getRecords("select languagestring_ID as 'ID', languagestring_name as 'String Name', languagestring_text as 'String Text' from language_strings where language_ID ='1'"); Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/#findComment-461398 Share on other sites More sharing options...
teng84 Posted February 8, 2008 Share Posted February 8, 2008 $array = array('one','two','three'); foreach($array as $val){ $$val = 'array var'.$x++; } echo $one; echo $two; echo $three; is this what you mean? Quote Link to comment https://forums.phpfreaks.com/topic/89986-setting-variables-dynamically/#findComment-461404 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.