Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere PHP API » Writing PHP scripts

Retrieving data from a database Next Page

Web forms


PHP can take user input from a web form, pass it to the database server as a SQL query, and display the result that is returned. The following example demonstrates a simple web form that gives the user the ability to query the sample database using SQL statements and display the results in an HTML table.

The source code for this sample is contained in your SQL Anywhere installation in a file called webisql.php.

<?
  echo "<HTML>\n";
  $qname = $_POST["qname"];
  $qname = str_replace( "\\", "", $qname );
  echo "<form method=post action=webisql.php>\n";
  echo "<br>Query: <input type=text Size=80 name=qname value=\"$qname\">\n";
  echo "<input type=submit>\n";
  echo "</form>\n";
  echo "<HR><br>\n";
  if( ! $qname ) {
      echo "No Current Query\n";
      return; 
  }
  # Connect to the database
  $con_str = "UID=DBA;PWD=sql;ENG=demo;LINKS=tcpip";
  $conn = sqlanywhere_connect( $con_str );
  if( ! $conn ) {
      echo "sqlanywhere_connect failed\n";
      echo "</html>\n";
      return 0;
  }
  $qname = str_replace( "\\", "", $qname );
  $result = sqlanywhere_query( $conn, $qname );
  if( ! $result ) {
        echo "sqlanywhere_query failed!";
  } else {
        // echo "query completed successfully\n";
        sqlanywhere_result_all( $result, "border=1" );
      sqlanywhere_free_result( $result );
  }
  sqlanywhere_disconnect( $conn );
  echo "</html>\n";
?>

This design could be extended to handle complex web forms by formulating customized SQL queries based on the values entered by the user.