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 脚本

 

连接到数据库

要建立一个到数据库的连接,将一个标准的 SQL Anywhere 连接字符串作为 sasql_connect 函数的参数传递给数据库服务器。<?php 和 ?> 标志指示 Web 服务器应让 PHP 执行这两个标志之间的代码,并用 PHP 输出替换这些代码。

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

<?php
  # Connect using the default user ID and password
  $conn = sasql_connect( "UID=DBA;PWD=sql" );
  if( ! $conn ) {
      echo "Connection failed\n";
  } else {
      echo "Connected successfully\n";
      sasql_close( $conn );
  }?>

此脚本会尝试与本地服务器上的数据库建立连接。要使此代码成功,必须在本地服务器上启动 SQL Anywhere 示例数据库或有相同证书的数据库。