简体   繁体   English

PHP从Mysql数据库创建变量列表

[英]PHP Creating variables list from Mysql database

I've a database that using for 3 languages this way: 我有一个数据库,它以这种方式用于3种语言:

id  name    de_de   al_sq
1   title   titel   titulli
2   points  punkte  pike

Now when in php $lang is set to 'al_sq': 现在在php中将$ lang设置为'al_sq':

$lang = 'al_sq';

I'm trying to generate variables using names of that language, in this case: 我正在尝试使用该语言的名称生成变量,在这种情况下:

$langtitle = 'titulli';
$langpoints = 'pike';

Trying something like: 尝试类似的东西:

$sql = "SELECT * FROM `langs`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    $lang{$row["lang"]} = $row[$lang];
  }
}

but something is not good, how to generate these variables? 但是有些不好,如何生成这些变量?

It seems you are using the database as a key-value store with multiple value fields depending on the language, you could use PDO::FETCH_KEY_PAIR so it returns an array with the name as the key. 似乎您将数据库用作具有多个值字段的键值存储,具体取决于语言,您可以使用PDO::FETCH_KEY_PAIR以便它返回名称为键的数组。 This way you also avoid loading the data for other languages that you might not need at all: 这样,您还可以避免加载可能根本不需要的其他语言的数据:

    $query = "SELECT `name`, :column as value FROM `langs`";
    $statement = $pdo->prepare($query);
    $statement->execute(["column"  => $lang]);
    $data = $statement->fetchAll(\PDO::FETCH_KEY_PAIR);

    // $data becomes an array with the name as the key:
    $langtitle = $data['title'];
    $langpoints = $data['points'];

Make sure, if the user provides the value for $lang , to check that it is a valid column value. 如果用户提供了$lang的值,请确保检查它是否是有效的列值。

This should be close to what you are wanting based on how your database is presented. 根据数据库的显示方式,这应该接近您想要的内容。 Scaling up would be clunky though if there is more the title and points stored. 如果存储更多的标题和点,则按比例放大将很麻烦。

$lang = 'al_sq';
$sql = "SELECT $lang FROM 'langs'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
 while($row = $result->fetch_assoc()) {
 $langtitle = $row[1];
 $langpoints = $row[2];
 }
}

You have a minor syntax error in your code, which causes an error. 您的代码中有一个较小的语法错误,这会导致错误。 What you need to do here is to: 您需要在此处执行以下操作:

  1. Fetch the name and al_sq columns from the database table. 从数据库表中获取nameal_sq列。 This is done by selecting the value of $lang (based on your code). 这是通过选择$lang的值(基于您的代码)来完成的。 For security reasons, the value of $lang is protected against SQL injection, as you did not specify where the value was coming from. 出于安全原因, $lang的值可以防止SQL注入,因为您没有指定值的来源。

  2. Then you must check if there was any results, and in the case there wasn't any, it will simply terminate the script with an error. 然后,您必须检查是否有任何结果,并且在没有结果的情况下,它只会以错误终止脚本。

  3. Then lastly you must iterate over each row of the returned results, and do a variable variable assignment. 最后,您必须遍历返回结果的每一行,并进行变量变量分配。 This will make $langpoints etc. work (and any other you may add in the future). 这将使$langpoints等起作用(以及将来可能添加的任何其他$langpoints )。

Code: 码:

$sql = 'SELECT `name`, `' . $conn->real_escape_string($lang) . '` FROM `langs`';

$result = $conn->query($sql);

if (!$result || !$result->num_rows) {
  echo 'Invalid language selected';
  exit;
}

while ($phrase = $result->fetch_assoc()) {
  ${'lang' . $phrase['name']} = $phrase[$lang];
}

// $langtitle
// $langpoints

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

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