Home > Articles > Data > Access

  • Print
  • + Share This
Like this article? We recommend

PHP Access of MySQL Tables

Now that we've introduced developing VoiceXML applications with Java servlets that can access a database, let's take a look at PHP. Like MySQL, PHP is popular among ISPs. The syntax will be familiar to UNIX developers.

For this program, you simply rewrite the example you used for Java in Books.java:

Books.php
<?xml version="1.0"?>
<vxml version="1.0">

<form>
<?php
 $db = mysql_connect("localhost", "");
 mysql_select_db("Test",$db);
 $query = "SELECT Title FROM Books WHERE Topics LIKE \"%$keyword%\"";
 $result = mysql_query( $query, $db);
 $numMatches = mysql_num_rows( $result );
 if( $numMatches == 0 )
 {
  echo "<block>No books on that subject were found.</block>";
 }
 elseif( $numMatches == 1 )
 {
  echo "<block>We have one book on this subject. It is \n";
  echo mysql_result( $result, 0 );
 }
 else
 {
  echo "<block>We have $numMatches related books. They are ";
  while( $row = mysql_fetch_array( $result ))
  {
   echo "\n".$row["Title"]."\n<pause>500</pause>";
  }
 }
 echo "\n<break msecs="500"/>\n";
 echo "</block>\n";
 mysql_free_result ($result);
?>
</form>
</vxml>

You can execute this program just like Books.java, either by examining the results in a browser source window or by linking from a VSP to your Books.php and BookSearch.vxml files running on a properly configured web host.

These programs are meant to introduce the concepts of server-side programming with VoiceXML and will get you started. They're not necessarily the best way to build business applications with these tools. One obvious caveat that we've ignored for the sake of being concise is that non-trivial applications should be layered to keep the business logic and user interface from being tightly coupled. If you follow this practice from HTML and WAP, you already have a library of data components to support these interfaces that you can now build VoiceXML atop relatively easily.

  • + Share This
  • 🔖 Save To Your Account