Forum Moderators: coopster

Message Too Old, No Replies

php variables inside mysql data

         

branmh

1:27 pm on Oct 10, 2021 (gmt 0)

10+ Year Member



I'm trying to convert a flat file txt database over to mysql, but I'm unable to convert the variables over to the a valve.

On another projects I used this code in the past:
$this->url = preg_replace("/\{(\w+)\}/e", '$$1', $url);


And now I'm using along with an array to convert the variables to valves:
 $url = preg_replace_callback('/\{(\w+)\}/', function($match) use ($values) {if isset($values[$match[1]])) {return $values[$match[1]];} else {return $match[0]; }},$url);


The only issue with doing this is, there will be a lot of items converted into array and cause of errors if not there.

Can someone explain a way to do this better and with less user error?


Thanks in advance.

NickMNS

5:45 pm on Oct 10, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I do not have answer to your question, and feel free to ignore this advice.

In programming there is the concept of "separation of concerns", which basically means that different aspects of your code should be clearly separated. The most basic application of this is separating data from code. Adding "PHP" variables to mySQL is doing the exact opposite. You should add the value of the variable to the database, and then write the code in PHP to retrieve the value from the db and assign it to the PHP variable.

The data in the database should not be dependent on the programming language. Like that if at some future point you decide to update your website from PHP to say Node.js you will be able to do it without needing to update all the data in your database. Ironically, this is the reason your are struggling to make the switch from the flat file txt to mySQL. I would strongly suggest that you invest the time now to separate your "data" from the code. You are re-writing code now anyways, so now is the time to do it.

Good look.

more about SoC:
[en.wikipedia.org...]

branmh

1:26 pm on Oct 11, 2021 (gmt 0)

10+ Year Member



ok thanks, I figured I would have to leave the flat file database files as is.

mack

8:36 pm on Dec 4, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I may be misunderstanding the question, but having the array in MySQL is quite possibly a bad idea. Just focus on adding the value then select it and build the array within your PHP code.

Mack.