Home > Backend Development > PHP Tutorial > Several ways to connect different databases in php

Several ways to connect different databases in php

WBOY
Release: 2016-07-25 09:03:14
Original
1500 people have browsed it
  1. $conn = new Com("ADODB.Connection"); //Instantiate a Connection object

  2. $connstr = "provider=sqloledb;datasource=.;uid=sa;pwd=123456 ;database=jnold;";
  3. $conn->Open($connstr);
  4. $rs = new Com("ADODB.Recordset"); //Instantiate a Recordcount object

  5. $rs->Open('select * from News where bigclassid = 59 And LeadPostil is null', $conn, 1, 1);

  6. $count = $rs->RecordCount;
  7. echo "There are {$count} items in total Record
    ";
  8. for($i = 0; $i < $count ; $i++){
  9. $arr_result[$i]['Title'] = addslashes($rs->Fields( 'Title')->Value);//Title
  10. $arr_result[$i]['Color'] = addslashes($rs->Fields('titlecolor')->Value?$rs->Fields ('titlecolor')->Value:'');//Title color
  11. $arr_result[$i]['WenHao'] = addslashes($rs->Fields('OtherText')->Value); //Document number
  12. }

Copy code

2.ODBC connection mssql

  1. $dbhost = '';

  2. $dbuser = ''; //Your mssql username
  3. $dbpass = ''; //Your mssql password
  4. $dbname = '' ; //Your mssql library name

  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' ] //Get field value
  11. ...
  12. }

Copy code

3.PHP built-in function connection Open the php.ini file on the server with php5 and apache. Remove the semicolon ";" in front of ;extension=php_mssql.dll. Just restart the apache server Secondly: Apply the sp3 patch to the server database where sqlserver2000 is installed, because with the sp3 patch, port 1433 can be opened. Finally, install the sqlserver2000 client tool on the web server

  1. $dbh=mssql_connect("192.168.12.124","sa","");
  2. mssql_select_db("mydb", $dbh);
  3. ?>
Copy code

This method often fails to connect. It seems to be a dll file version problem

4. Method to connect access data

  1. $db=$_SERVER['DOCUMENT_ROOT']."/PHP_ACCESS/include/#mydb.mdb"; //It is best to use $_SERVER['DOCUMENT_ROOT'] to get the path here
  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. /*Release resources*/
  11. $rs->Close();
  12. $conn->Close();
  13. $rs = null;
  14. $conn = null;
Copy code

5. Connect to MySQL method:

  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());

Copy code

6. How to connect to SQLserver

  1. $dbhost = 'localhost';
  2. $dbuser = 'sa'; //Your mssql username
  3. $dbpass = '1234'; //Your mssql password
  4. $dbname = '0772fang'; //Your mssql library name
  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);
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template