Jump to content

Setting Variables Dynamically


random1

Recommended Posts

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 :D

Link to comment
Share on other sites

Thanks :D

 

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?

Link to comment
Share on other sites

 

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'");

 

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.