Some basic PHP calls for use in Delphi for PHP or pure PHP

I was doing some coding today and couldn’t find some routines I’ve used often in the past.  Since I had to search for them, obeying my rule, they get posted here.  Maybe they can be of use to others.  These are examples I’ve written and once I’ve collected and customized.  I’ve not put comments in most of these routines, sorry.

(THIS IS A LIVING POST AND WILL CHANGE PERIODICALY AS I DIG UP OTHER COMMON ROUTINES)

Clean an array of all vulnerabilities:

[php]

function strip_and_slash_deep($value) {
return is_array($value) ?
array_map(‘strip_and_slash_deep’, $value) :
strip_tags($value);
addslashes($value);
}

[/php]

Use:

[php]

$protected_post_vars = array();

$protected_post_vars = strip_and_slash_deep($_POST);

[/php]

My print_r replacement to print an array in full depth:

[php]

function print_array($array)  {
if (is_array($array))
{
reset($array);
while (list($key, $val) = each($array))
{
if (is_array($val))
{
while (list($akey,$aval) = each($val))
{
$array[$key][$akey] = strip_tags($aval);
echo “Array: “.$key . “=” . htmlspecialchars($array[$key][$akey]).”<br/>”;
}
}
else
{
$array[$key] = strip_tags($val);
echo “Val: ” .$key . “=” . htmlspecialchars($array[$key]).”<br/>”;
}
}
}
else print($array);
}
[/php]

Basic structure for using PHP to connect to an interbase or firebird database:

[php]

function ib_execsql ($sql) {
$aresult = array();
$fulldbpath = “localhost:d:\blah.gdb”;
$username = “sysdba”;
$password = “masterkey”;
$ib = ibase_connect ($fulldbpath, $username, $password,’NONE’, ‘100’, ‘1’);
if ($ib===false) echo “No Connection”;
$result = @ibase_query ($ib, $sql);
if ($result===false) echo “<hr/>Error ‘”.ibase_errmsg().”‘ while executing ‘”. $sql.”‘<hr/>”;
while ($row = ibase_fetch_row ($result)) $aresult[] = $row;
ibase_close ($ib);
return $aresult;
}

[/php]

use:

[php]

$array = ib_execsql(“select * from whatever”);
print_array($array);

[/php]

Run a single query across many databases on the same MySQL server:

[PHP]

function execdbsql ($databasename) {
$link = mysql_connect(‘SERVERNAME’, “USER”, “PASS”);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db ( $databasename );

$result = mysql_query(‘SELECT desiredfield FROM tablename where field=”keyvalue”‘);

if (!$result) {
return;  // Don’t error out just skip this database.
}

echo mysql_result($result, 0).”<br/>”;

mysql_close($link);
}

echo “Values:<br/>”;

execdbsql (‘firstdb’);

execdbsql (‘seconddb’);

execdbsql (‘thirddb’);

[/PHP]

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *