php 連線資料庫的兩種方式

2021-09-01 12:04:47 字數 892 閱讀 4675

善於總結,是乙個好的學習方式。廢話少說,下面直接粘**。

一、最簡單的連線資料庫(php版本小於等於5.45)

<?php

header("content-type:text/html;charset=utf-8"); // 防止亂碼

$dbname='exam_database'; // 資料庫名

$host='localhost'; // 資料庫伺服器名

$user='root'; // 使用者名稱

$password='root'; // 密碼

$link=mysql_connect($host, $user, $password);

if (!$link)

if (!mysql_select_db($dbname, $link))

mysql_query("set names utf8");

?>

二、php 版本等於大於5.5 ,mysqli

使用第二種連線資料庫的方式

<?php

$conn = mysqli_connect("localhost","root","root","exam_database"); //  mysql 伺服器的連線

// 檢查連線 

if (! $conn)

mysqli_query($conn,"set names utf8");// 設定uft8,防止亂碼

$sql = "select * from users"; // sql語句

$result = mysqli_query($conn,$sql); // 執行sql語句,並將返回到結果集中

$row = mysqli_fetch_row($result); // 執行一條資料

print_r($row);// 列印輸出結果

jdbc連線資料庫的兩種方式

據我所知,使用jdbc連線上oracle有兩種方法 1 使用thin連線 然後就是設定連線字串了。這個是固定的寫法,如下所示 class.forname oracle.jdbc.driver.oracledriver connection conn drivermanager.getconnecti...

Excel 連線資料庫的兩種方式

1.mysql for excel 2.vba 連線資料庫 官方文件 總結 sub testconnecttodb dim sht1 as worksheet set sht1 worksheets sheet1 dim conn as adodb.connection dim rs as adod...

補 JSP連線資料庫的兩種方式

driver mydriver driver class.forname 資料驅動 取得資料庫連線,資料來源方式 context mycontext new initial context datasource mydatasource datasource mycontext.loopup def...