简体   繁体   English

MySQL数据库中的动态变量

[英]Dynamic Variables from MySQL Database

I have currently hit a dead-end with my coding, I am rewriting a huge chunk of code that is related to both changing site wide options that are stored in a database, and also the same for applying permissions on individual users accounts. 目前,我的编码工作陷入僵局,我正在重写大量的代码,这些代码既与更改存储在数据库中的站点范围选项有关,也与对单个用户帐户应用权限有关。

On the settings page, I have various settings stored in a database that I want to retrieve from the database to form my page, with the results I have retrieved I then populate a select box for each individual setting with "enabled" or "disabled", as most of the settings are either 0 or 1. 在“设置”页面上,我在数据库中存储了各种设置,我希望从数据库中检索这些设置以构成我的页面,获得的搜索结果然后为每个单独的设置填充一个“启用”或“禁用”的选择框,因为大多数设置为0或1。

My question is, what is the best way to fetch the data from the database and create dynamic variables to then use in my settings page? 我的问题是,从数据库中获取数据并创建动态变量然后在“设置”页面中使用的最佳方法是什么? The structure of the table is: id | 该表的结构为: id | setting_name | setting_name | setting_value . setting_value I only need setting_name and value, and I would like it if each variable that is made within the while loop was as follows; 我只需要setting_name和value,如果在while循环中创建的每个变量都如下所示,我将需要它。

setting_name = setting_value; setting_name = setting_value;

This is so then I can use that variable to populate my select box. 这样一来,我便可以使用该变量填充选择框。

$sql_query = mysql_query( "SELECT * FROM `setting`" );
while( $row = mysql_fetch_assoc( $sql_query ) )
{           
$row['name'] = $row['value'];
}

I thought the code above would work, however the variable is never created. 我以为上面的代码可以工作,但是从未创建变量。

Thanks for any help, 谢谢你的帮助,

再增加一个$: $$row['name'] = $row['value']

You want to do this: 您想这样做:

${$row[name]} = $row[value];

Also, you want to stop using mysql_ functions as they are being deprecated . 另外, 由于不推荐使用mysql_函数,因此您想停止使用它们。 You should use mysqli_ or PDO functions. 您应该使用mysqli_PDO函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM