我有一个多语言脚本,其工作方式如下:
en.php
define('lang_hello_world', 'Hello World!');
的index.php
<?php include('en.php'); echo lang_hello_world; ?>
该脚本工作正常并替换语言。但现在我需要转换存储在MySQL上的常量。
lang_hello_world
在MySQL中,我需要在页面上打印转换,我该怎么做?
答案 0 :(得分:0)
在en.php中,您需要连接数据库并定义值,如下所示:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = mysqli->query("SELECT * FROM language_table WHERE language = 'EN' LIMIT 1");
$row = $result->fetch_assoc();
define('lang_hello_world', $row[0]['lang_hello_world']);
希望可以提供帮助!
答案 1 :(得分:0)
使用constant()。如果从数据库中获取的$row['var']
等于lang_hello_world
,则:
$val = constant($row['var']);
如果加载了英文文件,则$val
等于Hello World!
。或者显然:
echo constant($row['var']);
答案 2 :(得分:-2)
在将它放入常量之前,只需将其从数据库中取出来?
<?php
mysql_connect("localhost", "root", "passw");
mysql_select_db("languages");
$query = mysql_query("SELECT lang_hello_world FROM languages");
$fetch = mysql_fetch_assoc($query);
define("lang_hello_world", $fetch['lang_hello_world']);
mysql_close();
?>