PHP 連線資料庫

2021-10-04 11:32:18 字數 1563 閱讀 7770

通過php做mysql的客戶端

1.2.1 開啟mysqli擴充套件

在php.ini中開啟mysqli擴充套件

extension=php_mysqli.dll
開啟擴充套件後重啟伺服器,就可以使用mysqli_函式了,

1.2.2 連線資料庫

建立news資料庫

-- 建立表

drop table if exists news;

create table news(

id int unsigned auto_increment primary key comment '主鍵',

title varchar(20) not null comment '標題',

content text not null comment '內容',

createtime int not null comment '新增時間'

)engine=innodb charset=utf8 comment '新聞表';

-- 插入測試資料

insert into news values (null,'鋤禾','鋤禾日當午',unix_timestamp());

insert into news values (null,'草','離離原上草',unix_timestamp());

思考:時間字段可以用datetime型別,也可以使用int型別。一般用int,因為datetime占用8個位元組,int占用4個位元組。

連線資料庫

mysqli_connect

(主機ip,使用者名稱,密碼,資料庫名,埠號)

//如果埠號是3306可以省略

mysqli_connect_error

():獲取連線資料庫的錯誤資訊

mysqli_connect_errno

():獲取連線資料庫的錯誤編碼

mysqli_set_charset

(連線物件,字元編碼)

**如下:

<?php 

//連線資料庫,連線成功返回連線物件

$link

=@mysqli_connect

('localhost'

,'root'

,'root'

,'data'

,'3306');

//var_dump($link); //object(mysqli)if(

mysqli_connect_error()

)//設定字元編碼

mysqli_set_charset

($link

,'utf8'

);

php連線資料庫

create table message id tinyint 1 not null auto increment,user varchar 25 not null,title varchar 50 not null,content tinytext not null,lastdate date n...

php連線資料庫

天貓內部優惠券 設定資料庫變數 db host localhost 資料庫主機名稱,一般都為localhost db user root 資料庫使用者帳號,根據個人情況而定 db passw 資料庫使用者密碼,根據個人情況而定 db name test 資料庫具體名稱,以剛才建立的資料庫為準 連線資...

PHP連線資料庫

php連線資料庫函式 mysql connect 開啟mysql連線 mysql select db 開啟乙個資料庫 和or die 隱藏錯誤和條件顯示 mysql connect 主機 使用者名稱 密碼 mysql select db 開啟資料庫 鏈結標示符 如果不是特宣告連線標示符,則預設為是上...