欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php连接不同数据库的几种方法

程序员文章站 2023-12-22 22:18:34
...
  1. $conn = new Com("ADODB.Connection"); //实例化一个Connection对象

  2. $connstr = "provider=sqloledb;datasource=.;uid=sa;pwd=123456;database=jnold;";
  3. $conn->Open($connstr);
  4. $rs = new Com("ADODB.Recordset"); //实例化一个Recordcount对象
  5. $rs->Open('select * from News where bigclassid = 59 And LeadPostil is null', $conn, 1, 1);

  6. $count = $rs->RecordCount;
  7. echo "共有{$count}条纪录
    ";
  8. for($i = 0; $i $arr_result[$i]['Title'] = addslashes($rs->Fields('Title')->Value);//标题
  9. $arr_result[$i]['Color'] = addslashes($rs->Fields('titlecolor')->Value?$rs->Fields('titlecolor')->Value:'');//标题颜色
  10. $arr_result[$i]['WenHao'] = addslashes($rs->Fields('OtherText')->Value);//文号
  11. }
复制代码

2.ODBC连接mssql

  1. $dbhost = '';

  2. $dbuser = ''; //你的mssql用户名
  3. $dbpass = ''; //你的mssql密码
  4. $dbname = ''; //你的mssql库名
  5. $connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname","$dbuser","$dbpass");

  6. $sql="select * from content";
  7. $exec=odbc_exec($connect,$sql);
  8. while($row = (odbc_fetch_array($exec)))
  9. {
  10. $row['id'] //獲取字段值
  11. ...
  12. }
复制代码

3.PHP内置函数连接 打开装有php5和apache的服务器上的php.ini文件 去掉;extension=php_mssql.dll前面的分号";" 重启apache服务器即可 其次:将安装有sqlserver2000的服务器数据库打上sp3补丁,因为打上sp3补丁,可以开启1433端口. 最后,web服务器上安装sqlserver2000客户端工具

  1. $dbh=mssql_connect("192.168.12.124","sa","");
  2. mssql_select_db("mydb", $dbh);
  3. ?>
复制代码

这个方法经常会连接不上,好像是一个dll文件版本的问题

4.连接access数据的方法

  1. $db=$_SERVER['DOCUMENT_ROOT']."/PHP_ACCESS/include/#mydb.mdb"; //這裏最好用$_SERVER['DOCUMENT_ROOT']獲取路徑
  2. $conn = new COM('ADODB.Connection') or die('can not start Active X Data Objects');
  3. $conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
  4. $rs = $conn->Execute('SELECT * FROM contents order by id desc');
  5. while(!$rs->EOF)
  6. {
  7. echo $rs->Fields['name']->Value;
  8. $rs->MoveNext();
  9. }
  10. /*释放资源*/
  11. $rs->Close();
  12. $conn->Close();
  13. $rs = null;
  14. $conn = null;
复制代码

5.连接MySQL方法:

  1. $database_connection=null;

  2. $hostname="localhost";

  3. $database="5aart";
  4. $username="root";
  5. $password="1234";
  6. global $database_connection;
  7. $database_connection=mysql_connect($hostname,$username,$password) or die(mysql_error());
  8. mysql_query("set names 'gbk'");
  9. mysql_select_db($database,$database_connection) or die(mysql_error());
复制代码

6.连接SQLserver的方法

  1. $dbhost = 'localhost';
  2. $dbuser = 'sa'; //你的mssql用户名
  3. $dbpass = '1234'; //你的mssql密码
  4. $dbname = '0772fang'; //你的mssql库名
  5. $connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname","$dbuser","$dbpass");
  6. $sql="update news_pk set ffnums=ffnums+1 where newsID='$ID'";
  7. $exec=odbc_exec($connect,$sql);
复制代码

上一篇:

下一篇: