[英]Define constant PHP
我有一个像这样的多语言脚本:
zh.php
define('lang_hello_world', 'Hello World!');
index.php
<?php include('en.php'); echo lang_hello_world; ?>
该脚本可以正常工作并替换语言。 但是现在我需要转换存储在MySQL上的常量。
lang_hello_world
在MySQL中,我需要在页面上打印转换后的图像,我该怎么做?
在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']);
希望能对您有所帮助!
使用constant() 。 如果从数据库中获取的$row['var']
等于lang_hello_world
则:
$val = constant($row['var']);
现在$val
等于Hello World!
如果已加载英文文件。 或显然:
echo constant($row['var']);
只是将其从数据库中获取,然后再将其放入常量中?
<?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();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.