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

SAP Sybase SQL Anywhere 16.0 (中文) » SQL Anywhere 服务器 - 编程 » PHP 支持 » SQL Anywhere PHP 扩展 » PHP 脚本开发

 

Web 表单

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

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



<?php
    echo "<HTML>\n";
    echo "<body onload=\"document.getElementById('qbox').focus()\">\n";

    $qname = $_POST[qname];
    $qname = str_replace( "\\", "", $qname );
    echo "<form method=post action=webisql.php>\n";
    echo "<br>Query : <input id=qbox 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;
    }

    $conn = sasql_connect( "UID=DBA;PWD=sql" );

    if( ! $conn ) {
        echo "sasql_connect failed\n";
    } else {

        $qname = str_replace( "\\", "", $qname );
        $result = sasql_query( $conn, $qname );

        if( ! $result ) {
            echo "sasql_query failed!";
        } else {

            if( sasql_field_count( $conn ) > 0 ) {
                sasql_result_all( $result, "border=1" );
                sasql_free_result( $result );
            } else {
                echo "The statement <h3>$qname></h3> executed successfully!";
            }
        }

        sasql_close( $conn );
    }

    echo "</body>\n";
    echo "</html>\n";
?>

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