I have a multi-language script that works like this:
en.php
define('lang_hello_world', 'Hello World!');
index.php
<?php include('en.php'); echo lang_hello_world; ?>
The script works fine and replace the language. But now I need to convert the constant that are stored on MySQL.
The lang_hello_world
is in MySQL and I need to print converted on the page, how can I do this?
In the en.php, you need connect the database and define the value, something like this:
$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']);
Hope can help!
Use constant() . If $row['var']
fetched from the database equals lang_hello_world
then:
$val = constant($row['var']);
Now $val
equals Hello World!
if the English file is loaded. Or obviously:
echo constant($row['var']);
Just grab it out of your database before you put it in your constant?
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.