Công ty Thiết kế web Thiên Việt | Dịch vụ Thiết kế website | Web Design | Quảng bá web top 10 Google | Search Engine Optimition
 
   Trang Chủ  
  Giới Thiệu  

Kiểm tra Tên miền

  Liên Hệ  
  Quảng cáo Miễn Phí  
  Thư Giãn  
  Diễn đàn  
 
 
Liên Hệ Thiết Kế Website
 
     Hỏi đáp trực tuyến
 
 
 
DSN and DSN-less connections
DSN and DSN-less connections Print Email

DSN stands for 'Data Source Name'. It is an easy way to assign useful and easily rememberable names to data sources which may not be limited to databases alone. If you do not know how to set up a system DSN read our tutorial How to set up a system DSN.

For our example below lets assume that our DSN points to an Access database called 'examples.mdb' and that we will be selecting records from the table 'cars'.

<?php

//connect to a DSN "myDSN"

$conn = odbc_connect('myDSN','','');

if ($conn)
{
  //the SQL statement that will query the database
  $query = "select * from cars";
  //perform the query
  $result=odbc_exec($conn, $query);

  echo "<table border=\"1\"><tr>";

  //print field name
  $colName = odbc_num_fields($result);
  for ($j=1; $j<= $colName; $j++)
  { 
    echo "<th>";
    echo odbc_field_name ($result, $j );
    echo "</th>";
  }

  //fetch tha data from the database
  while(odbc_fetch_row($result))
  {
    echo "<tr>";
    for($i=1;$i<=odbc_num_fields($result);$i++) 
    {
      echo "<td>";
      echo odbc_result($result,$i);
      echo "</td>";
    }
    echo "</tr>";
  }

  echo "</td> </tr>";
  echo "</table >";

  //close the connection
  odbc_close ($conn);
}
else echo "odbc not connected";
?>

Remember that 'myDSN' above is name of the DSN. Also note that you can change the table name from 'cars' to the name of your table and point the DSN to whatever database you like. One other thing to remember is that you can set up a DSN on your own machine though if you are using a hosting company you may have to ask the webmaster.

DSN-less connection

DSN-less connections don't require creation of system level DSNs for connecting to databases and provide an alternative to DSNs. We will now see how to connect to a database via PHP using Connection String in place of DSN name.

<?php

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection")
  or die("Cannot start ADO");

//define connection string, specify database driver
$connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= c:\inetpub\wwwroot\db\examples.mdb";
  $conn->open($connStr);
//Open the connection to the database

//declare the SQL statement that will query the database
$query = "SELECT * FROM cars";

//execute the SQL statement and return records
$rs = $conn->execute($query);

$num_columns = $rs->Fields->Count();
echo $num_columns . "<br>"; 

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

echo "<table>"; while (!$rs->EOF) 
//carry on looping through while there are records
{
    echo "<tr>";
    for ($i=0; $i < $num_columns; $i++) {
        echo "<td>" . $fld[$i]->value . "</td>";
    }
    echo "</tr>";
    $rs->MoveNext(); //move on to the next record
}


echo "</table>";

//close the connection and recordset objects freeing up resources 
$rs->Close();
$conn->Close();

$rs = null;
$conn = null;
?>
Contents provided by Thiet ke web Thiên Việt
    
Gởi trang này cho bạn bè Ghi nhớ trang này
Ý kiến mới nhất
 
 
 
Đóng góp ý kiến
 
 
Tên gọi
Website
Email
Nội dung
=
 
1 2 3 4 5 6 7 8 9 10
 
 
     
 
   
 
   
   
 
Goc trai tren   Goc phai tren
 
Mẩu Web Tham Khảo
Nut thiet ke web Web Flash
 

 

Goc trai duoi   Goc phai duoi
 
 
Liên Kết Website
 
 
 
 
 
 
 
ĐỐI TÁC - KHÁCH HÀNG

Phòng kinh doanh
YM Yahoo Support
Mobile icon Mr.Lộc 0919.22.10.30
Email icon Email phuocloc@vietweblink.net
Hỗ trợ kỹ thuật:
YM Yahoo Support
Mobile icon Mr.Tranh 0168.69.12.5.88
Email icon Email tranh@vietweblink.net
 
 
Triển khai dịch vụ thiết kế web flash cao cấp dành cho các khách hàng muốn sở hữu một website tuyệt đẹp bất kỳ ai cũng phải khen ngợi khi chiêm ngưỡng. Tham khảo các mẫu thiết kế tại đây

Chương trình khuyến mãi

  Uong cafe o dau
 
 
  Mẩu Web Flash Cao Cấp
 
   
 

  Hiện đang có 33 khách online
 
 
Khuyen mai KHÁCH HÀNG TIỂU BIỂU
The gioi dong ho Mia ca Quang Thong Cong ty CBL Viet Nam May dong goi an Phuc Thanh Toan Cuoc Phi Thiep cuoi nhu y Ao cuoi nhu muoi
 
     
  Site Help | Tuyển dụng | Liên hệ | Khách Hàng | Thiết Kế Website | Quản trị website | Quảng bá website | Hỏi đáp trực tuyến | Liên kết website Click here to switch to other Office Online sites worldwide
Công ty Thiết Kế Web © 2009 THIÊN VIỆT WEB-Design. All rights reserved.