store all your code in MySql

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
julianb
Forum Newbie
Posts: 5
Joined: Wed Jul 21, 2010 3:40 pm

store all your code in MySql

Post 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>";
Last edited by julianb on Wed Aug 11, 2010 3:15 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: store all your code in MySql

Post 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.
julianb
Forum Newbie
Posts: 5
Joined: Wed Jul 21, 2010 3:40 pm

Re: store all your code in MySql

Post 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.
julianb
Forum Newbie
Posts: 5
Joined: Wed Jul 21, 2010 3:40 pm

Re: store all your code in MySql

Post 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).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: store all your code in MySql

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: store all your code in MySql

Post by Eran »

Don't you find editing code in a database considerably more cumbersome than using a text editor on text files?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: store all your code in MySql

Post 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
Hanse
Forum Newbie
Posts: 3
Joined: Mon Aug 30, 2010 3:00 pm

Re: store all your code in MySql

Post by Hanse »

-when you edit a function, copy the old code to a log so you can retrieve old versions of the function.
Git
Post Reply