[英]How to echo a PHP variable from MySQL results
To automate my PHP to generate JSON, I saved the name of variable in database (eg > $test_value_1
). 为了自动化我的PHP生成JSON,我在数据库中保存了变量的名称(例如> $test_value_1
)。
In my php file I have the value of this variable (eg > $test_value_1 = "TEST VALUE 1"
) 在我的php文件中,我有这个变量的值(例如> $test_value_1 = "TEST VALUE 1"
)
After this I do a query to echo this variable, but, instead of returning the value of variable (TEST VALUE 1), always return just the text save in database ("$teste_value_1") 在此之后,我执行查询以回显此变量,但是,不是返回变量的值(TEST VALUE 1),而是始终只返回数据库中的文本保存(“$ teste_value_1”)
To understand better, look my database, variable, query, response and what I need: 要更好地理解,请查看我的数据库,变量,查询,响应以及我需要的内容:
TABLE: attributes 表:属性
id_attribute | attribue_string | attribute_value
1 | test_string_1 | $test_value_1
2 | test_string_2 | $test_value_2
VARIABLES: 变量:
$test_value_1 = "Test Value 1";
$test_value_2 = "Teste Value 2";
QUERY: 查询:
$query_array = mysqli_query($connect,"
SELECT GROUP_CONCAT(CONCAT('{id:', a.attribute_string, ',value_name:', a.attribute_value, '}') SEPARATOR ', ') AS concat
FROM rel_categories_attributes AS rca
INNER JOIN categories AS c ON c.id_category = rca.categories_id_category
INNER JOIN attributes AS a ON a.id_attribute = rca.attributes_id_attribute
WHERE id_category = '{$id_category}'
");
WHILE ($reg_cat = mysqli_fetch_array($query_array)){
echo $teste_query = $reg_cat["concat"] . ",";
RESPONSE: {id:test_string_1,value_name:$test_value_1}, {id:teste_string_2,value_name:$test_value_2}
, (WRONG) 回复: {id:test_string_1,value_name:$test_value_1}, {id:teste_string_2,value_name:$test_value_2}
,(错误)
WHAT I NEED: {id:test_string_1,value_name:TEST VALUE 1}, {id:teste_string_2,value_name:TESTE VALUE 2}
, 我需要什么: {id:test_string_1,value_name:TEST VALUE 1}, {id:teste_string_2,value_name:TESTE VALUE 2}
,
While this seems like a horrible design, you can do it using variable variables . 虽然这看起来像一个可怕的设计,你可以使用变量变量 。 But you'll need to do the output formatting in PHP, not in MySQL. 但是你需要在PHP中进行输出格式化,而不是在MySQL中。
$query_array = mysqli_query($connect,"
SELECT a.attribute_string, a.attribute_value
FROM rel_categories_attributes AS rca
INNER JOIN categories AS c ON c.id_category = rca.categories_id_category
INNER JOIN attributes AS a ON a.id_attribute = rca.attributes_id_attribute
WHERE id_category = '{$id_category}'
");
$result = array();
while ($row = mysqli_fetch_assoc($query_array) {
$result[] = array('id' => $row['attribute_string'], 'value_name' => ${$row['attribute_value']});
}
echo json_encode($result);
However, variable variables can almost always be improved by using an associative array. 但是,通过使用关联数组,几乎总能改进变量变量。 Instead of having variables like $test_value_1
and $test_value_2
, create an array whose keys are "test_value_1"
and "test_value_2"
, and then use $array[$row['attribute_value']]
. 不要使用$test_value_1
和$test_value_2
等变量,而是创建一个键为"test_value_1"
和"test_value_2"
$array[$row['attribute_value']]
,然后使用$array[$row['attribute_value']]
。
But even better would be to put all the details in the database itself, rather than hard-coding them in the scripts. 但更好的方法是将所有细节都放在数据库本身,而不是在脚本中对它们进行硬编码。 You can then join with that table to translate the attribute value to the appropriate string. 然后,您可以与该表联接以将属性值转换为适当的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.