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

SQL Anywhere 12.0.0 (中文) » SQL Anywhere 服务器 - 编程 » PHP 支持 » SQL Anywhere PHP 扩展 » 编写 PHP 脚本

 

Web 表单

PHP 可以从 Web 表单获取用户输入,将用户输入作为 SQL 查询传递给数据库服务器,并显示返回的结果。以下示例介绍了一个简单的 Web 表单,该表单使用户能够使用 SQL 语句查询示例数据库,并将结果显示在一个 HTML 表中。

本示例的源代码包含在 SQL Anywhere 安装目录中一个名为 webisql.php 的文件中。



<?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;SERVER=demo;LINKS=tcpip";
  $conn = sasql_connect( $con_str );
  if( ! $conn ) {
      echo "sasql_connect failed\n";
      echo "</html>\n";
      return 0;
  }
  $qname = str_replace( "\\", "", $qname );
  $result = sasql_query( $conn, $qname );
  if( ! $result ) {
        echo "sasql_query failed!";
  } else {
        // echo "query completed successfully\n";
        sasql_result_all( $result, "border=1" );
      sasql_free_result( $result );
  }
  sasql_disconnect( $conn );
  echo "</html>\n";
?>

通过基于用户输入的值的公式化自定义 SQL 查询,可以扩展这一设计,从而处理复杂的 Web 表单。