Page 1 of 1

store all your code in MySql

Posted: Wed Aug 04, 2010 11:50 am
by julianb
I decided I wanted to keep my PHP code in a MySql database. I wanted to record a "modified date/time" for each function. Before using eval() on each function, code is added so that we send input variables that the function received, to the string called $_GLOBALS['dbug'] .

Some possibilities:
-display the code of MyFunction() in an editable text box. display a list of links to the functions that are called by MyFunction(). display a list of links to the functions that call MyFunction();
-optionally send the function's "return" value to the 'dbug' variable.
-sort the 'log of function calls & inputs' that we are putting in 'dbug' so that the most-recently-edited functions show first. so the function you are editing now, automatically shows you what information it is receiving when it is called.
-when you edit a function, copy the old code to a log so you can retrieve old versions of the function.


Code: Select all

function connectToDb2(){
$con = mysql_connect("localhost","mySqlUserName","thisIsAPassword");
$db_selected = mysql_select_db('testing123');
}

function getFunctions(){
$result=mysql_query("SELECT `time`,`name` FROM `edr`");
for($i = 0;$array[$i]=mysql_fetch_assoc($result);$i++){
$array[$i]=implode("\n",$array[$i]);
$array[$i]=transformFunctionsForDebug($array[$i]);
}

$array = "saveandrespond();\n" . implode("",$array);
eval($array);
//echo "<pre>";print_r(htmlspecialchars($array));echo "</pre>";
}

function transformFunctionsForDebug($function){
$functionTime=leftofdelim($function,"\n");
$function=rightofdelim($function,"\n");
$functionHead=leftofdelim($function,"{");
$functionCode=rightofdelim($function,"{");
$functionName=leftofdelim($functionHead,"(");
$functionName=str_replace("function ","",$functionName);
$vars=rightofdelim($functionHead,"(");
$vars=trim($vars,"()&\$ ");
$vars=explode("$",$vars);
$i=0;
foreach ($vars as $var){
$var = "\$" . trim($var,", &");
$va[$i]=leftofdelim($var,"=");
$i++;
}
$debugCode .= "\$GLOBALS['dbug'].= \"";
$debugCode .= "$functionTime $functionName ";
foreach($va as $var){
if ($var == "\$")continue;
$debugCode .= ltrim($var,"\$ ") . "- \" . print_r(" . $var . ",TRUE) . \" ";
}
$debugCode .= "\";";
return "$functionHead{ $debugCode " . ltrim($functionCode,"{"); 
}

function leftofdelim($string,$delim){
if ($delimPos=strpos($string,$delim))
$string = substr($string,0,$delimPos);
return $string;
}

function rightofdelim($string,$delim){
if ($delimPos=strpos($string,$delim))
$string = substr($string,$delimPos);
return $string;
}

Code: Select all

function setDebug(){
if (!$_POST[0]) 
$response .= "(no request)";
$response = ("PHP server received this request: $response" 
. $_POST[0] . $GLOBALS['dbug']);
$response=str_replace("\n",'\n',$response);
$response=str_replace("\r","",$response);
$response=htmlspecialchars($response);
echo <<<EOD
<script type="text/javascript"> 
function doMenu2(){
item=document.getElementById("tableChange").selectedIndex;
if (item == 0){
document.getElementById('DivExample').innerHTML = "<p>Table:<input type=\"text\" value=\'Table\' id=\'field1\' />Record Name:<input type=\"text\" value=\'Name\' id=\'field2\' />Relationship:<input type=\"text\" value=\'Relationship\' id=\'field3\' /><input type=\"hidden\" id=\'field4\' /><input type=\"hidden\" id=\'field5\' /><input id=\'Button1\' type=\'button\' value=\'Add Record\' onclick=\"collateSend(\'addRecord\')\" /></p>";
}
else if (item == 1)
{document.getElementById('DivExample').innerHTML = "<p>Table:<input type=\"text\" value=\'Table\' id=\'field1\' />Column Name:<input type=\"text\" value=\'ColumnName\' id=\'field2\' /><input type=\"hidden\" id=\'field3\' /><input type=\"hidden\" id=\'field4\' /><input type=\"hidden\" id=\'field5\' /><input id=\'Button1\' type=\'button\' value=\'Add Column\' onclick=\"collateSend(\'addColumn\')\" /></p>";}
else if (item == 6)
{document.getElementById('DivExample').innerHTML = "<p><textarea COLS=140 WRAP=SOFT ROWS=3>$response</textarea></p>";}
 
}
 
</script> 
EOD;
}

echo "<p><textarea>$response</textarea></p>";

Re: store all your code in MySql

Posted: Wed Aug 04, 2010 5:58 pm
by superdezign
Is this supposed to be a development tool, or do you plan to implement this a live feature? I'm pretty sure this functionality already exists in some form or another... SVN comes to mind.

Re: store all your code in MySql

Posted: Thu Aug 05, 2010 1:59 pm
by julianb
It's intended as a "live" development tool i guess?

the code I am storing in MySql is code for an address-list / "constituent relationship management" type application.

Re: store all your code in MySql

Posted: Tue Aug 10, 2010 11:16 am
by julianb
To clarify - one of my reasons for setting my code up this way was to have a whole set of code that is "debug" code - it gets inserted into the running code "live" when I am in "debug" mode but otherwise, it gets left out.

similarly, my code can be displayed on-screen with comments, or without comments...

or i can display all functions that are "underneath" the "foo" function (those that are called by "foo", those that are called by a function called by "foo" etc).

Re: store all your code in MySql

Posted: Mon Aug 30, 2010 5:25 am
by josh
Why? You can substitute code with regular files & gool old object oriented programming. Its called the 'strategy design pattern'. And it doesn't involve adding 1,000 database queries to your page.

Re: store all your code in MySql

Posted: Mon Aug 30, 2010 7:54 am
by Eran
Don't you find editing code in a database considerably more cumbersome than using a text editor on text files?

Re: store all your code in MySql

Posted: Thu Sep 02, 2010 12:13 pm
by josh
julianb wrote:display a list of links to the functions that are called by MyFunction(). display a list of links to the functions that call MyFunction();
PHPDocumentor
-optionally send the function's "return" value to the 'dbug' variable.
-sort the 'log of function calls & inputs' that we are putting in 'dbug' so that the most-recently-edited functions show first. so the function you are editing now, automatically shows you what information it is receiving when it is called.
XDebug
-when you edit a function, copy the old code to a log so you can retrieve old versions of the function.
subversion

Re: store all your code in MySql

Posted: Fri Sep 10, 2010 12:42 am
by Hanse
-when you edit a function, copy the old code to a log so you can retrieve old versions of the function.
Git