duanhuanyou6478 2017-06-12 06:10
浏览 321

如何在Mysql中处理带有嵌入变量的字符串

I am in need of some advice on how to best process code to keep things running efficiently while still returning dynamic strings to the user.

In my particular situation, I have a set of string responses that vary based on a set of six variables. Early in the project, it was easy to simply use nested if and switch statements. However, as the project advances, this set of code is becoming massive. I would very much like find a way to store this information in a database, but I am unaware of a way to do so that doesn't open up the database to vulnerabilities.

A sample script might look like the following:

printf("Let's say for a moment here, that I want to have a script that is something like this where, ". $yourname ." is printed as well as other pieces of information stored from a database such as: ".$yourfavoritefood." or ".$timesinceloggedon.".  Clearly this output will be different from person to person.");

Now imagine I have hundreds of these variable driven scripts. Is there a way to SECURELY store scripts in a mysql database so that I can query only the script I need given the program driven variables at the time?

  • 写回答

1条回答 默认 最新

  • dongzhong7299 2017-06-12 06:17
    关注

    You can store the text you create in the database or in a file and use printf with arguments to output the text, http://php.net/manual/en/function.printf.php,

    printf("Let's say for a moment here, that I want to have a script that is something like this where, :s is printed as well as other pieces of information stored from a database such as: :s or :s.  Clearly this output will be different from person to person.", [$yourname, $yourfavoritefood, $timesinceloggedon);
    

    By taking all parts that are different for different users into parameters you can find a common string which can be stored where you store translations

    评论

报告相同问题?