The Code Cave

April 1, 2007

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

Filed under: Delphi for PHP — Brian @ 6:33 pm

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:
  1. function strip_and_slash_deep($value) {
  2. return is_array($value) ?
  3. array_map(’strip_and_slash_deep’, $value) :
  4. strip_tags($value);
  5. addslashes($value);
  6. }

Use:

PHP:
  1. $protected_post_vars = array();
  2.  
  3. $protected_post_vars = strip_and_slash_deep($_POST);

My print_r replacement to print an array in full depth:

PHP:
  1. function print_array($array)  {
  2. if (is_array($array))
  3. {
  4. reset($array);
  5. while (list($key, $val) = each($array))
  6. {
  7. if (is_array($val))
  8. {
  9. while (list($akey,$aval) = each($val))
  10. {
  11. $array[$key][$akey] = strip_tags($aval);
  12. echo “Array: “.$key . “=” . htmlspecialchars($array[$key][$akey]).“<br/>”;
  13. }
  14. }
  15. else
  16. {
  17. $array[$key] = strip_tags($val);
  18. echo “Val: “ .$key . “=” . htmlspecialchars($array[$key]).“<br/>”;
  19. }
  20. }
  21. }
  22. else print($array);
  23. }

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

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

use:

PHP:
  1. $array = ib_execsql(“select * from whatever”);
  2. print_array($array);

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

PHP:
  1. function execdbsql ($databasename) {
  2. $link = mysql_connect(‘SERVERNAME’, “USER”, “PASS”);
  3. if (!$link) {
  4. die(‘Could not connect: ‘ . mysql_error());
  5. }
  6.  
  7. mysql_select_db ( $databasename );
  8.  
  9. $result = mysql_query(‘SELECT  desiredfield FROM tablename where field="keyvalue"’);
  10.  
  11. if (!$result) {
  12. return;  // Don’t error out just skip this database.
  13. }
  14.  
  15. echo mysql_result($result, 0).“<br/>”;
  16.  
  17. mysql_close($link);
  18. }
  19.  
  20. echo “Values:<br/>”;
  21.  
  22. execdbsql (‘firstdb’);
  23.  
  24. execdbsql (’seconddb’);
  25.  
  26. execdbsql (‘thirddb’);

5 Comments »

  1. $protected_post_vars = strip_and_slash_deep($HTTP_POST_VARS);

    May I suggest that the example should be $protected_post_vars = strip_and_slash_deep($_POST);

    Since $HTTP_POST_VARS is depreciated in PHP 5 and will be removed in PHP 6

    Comment by Stuart Kelly — April 1, 2007 @ 7:48 pm

  2. Done Stu, and thanks for the Tip!

    Comment by Brian — April 1, 2007 @ 9:32 pm

  3. Excellent! Thanks for the post -- I will cross post this article on my blog as well, as I personally find it helpful.

    Comment by OneNerd — April 1, 2007 @ 9:48 pm

  4. [...] 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. Permalink | Share (digg, etc):         [...]

    Pingback by Firebird News » Some basic PHP calls for use in Delphi for PHP or pure PHP — April 4, 2007 @ 10:25 am

  5. [...] Some excellent PHP code snippets at the code cave blog to use in your Delphi for PHP coding: http://www.thecodecave.com/article363 [...]

    Pingback by Some useful PHP Code Snippets : OneNerd’s Software Company — April 21, 2007 @ 5:11 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress