PHP Usage

Sometimes, for a carpenter, every solution involves a hammer. PHP is like that. It’s every web developers hammer. My problem is that many PHP based apps, because of the way they are written, are hard to understand and maintain. Also since PHP apps runs on the server side, they are always going to be slower than running JavaScript on the client side. Finally, a PHP app doesn’t fit the AJAX paradigm that I think is the future of the web.

Therefore my PHP usage is primarily as a data bridge to bring the other pieces (JavaScript, xhtml, MySQL) together. Therefore has no need for PHP frameworks because it’s just not that complicated. I want all my PHP to be straightforward, fast, short, simple to maintain. Every time I call the server (via AJAX) I execute PHP to fetch and format the data – and never for web page manipulation. requires PHP 5.2 or greater for built in XML support. Here’s a short example of a PHP script I use and how it builds the XML (parts are obfuscated for security reasons…).:

$MyPswd = "real password goes here...";
$DB_Connection = mysql_connect("localhost", "mackle_lds", $MyPswd) or die ('Cannot connect to database: '.mysql_error());

 // prevent SQL injection attacks with mysql_real_escape_string
$volume_id = mysql_real_escape_string($_GET["volume"]);
$book_id = mysql_real_escape_string($_GET["book"]);
$chapter = mysql_real_escape_string($_GET["chapter"]);

// Tell the browser the output is XML Unicode
header("Content-Type: text/xml; charset=utf-8");

// Query for volume and book
$sql  = "SELECT volume_title, num_chapters, book_title, book_title_long FROM scriptures_books WHERE ";
$sql .= "volume_id = $volume_id And book_id = $book_id";
$result = mysql_query($sql,$DB_Connection);

if ($row = mysql_fetch_object($result)) {
  //Create XML document using the DOM 
  $xmlDoc = new DomDocument('1.0', 'UTF-8');
  $scriptures->setAttribute('title','The Holy Scriptures');
  if ($SQL == 'Y') {$scriptures->setAttribute('sql',$sql2);} // Used for diagnostics in admin mode
  // Create volume sub-node                 
  $volume = $scriptures->appendChild($xmlDoc->createElement('volume')); 
  // Create book sub-node
  $book = $volume->appendChild($xmlDoc->createElement('book'));        
  // Query for chapter and verses
  $sql2 = "SELECT chapter, chapter_name, verse_id, verse, verse_scripture, html_top, html_right, html_bottom,";
  $sql2 .= " html_left, html_replace, groupname FROM lds_scriptures_verses ";
  $sql2 .= " WHERE volume_id = $volume_id And book_id = $book_id And chapter = $chapter";
  $result2 = mysql_query($sql2,$DB_Connection);
  $FirstTime = true;
  while ($row2 = mysql_fetch_object($result2)) {
     if ($FirstTime === true) {
       // Create the chapter sub-node. We are only doing one chapter of verses.
       $chapter = $book->appendChild($xmlDoc->createElement('chapter'));
       $FirstTime = false;
    if ($row2->html_replace === '') {
      // Format the standard version with formatting as defined, if any
      $verse_text = str_replace('\r\n','<br>',$row2->verse_scripture);
      $verse_text = $row2->html_top.$row2->html_left.$verse_text.$row2->html_right.$row2->html_bottom;
    } else {
      // Assign the special formatted verse,  rather than the raw text verse used for searching.
      $verse_text = $row2->html_replace;
    // Create the chapter verse sub-node and attributes
    $verse = $chapter->appendChild($xmlDoc->createElement('verse'));
  // Send results back to JavaScript Client
  echo $xmlDoc->saveXML();
  // Close the connection

Despite my remarks on the role of PHP, I enjoy PHP programming a lot. It’s is a great and powerful language. My next post will discuss the script above…

Leave a Reply

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