myssqli和mysql的函數(shù)是差不多的, 只不過(guò), mysqi可以用對(duì)象方式
1
2
3
4
5
6
//過(guò)程方式:
$Con = mysqli_connect('數(shù)據(jù)庫(kù)服務(wù)器地址', '數(shù)據(jù)庫(kù)登陸用戶(hù)', '數(shù)據(jù)庫(kù)密碼');
mysqli_select_db( $Con, '數(shù)據(jù)庫(kù)名稱(chēng)');
//對(duì)象方式
$Con = new mysqli('數(shù)據(jù)庫(kù)服務(wù)器地址', '數(shù)據(jù)庫(kù)登陸用戶(hù)', '數(shù)據(jù)庫(kù)密碼', '數(shù)據(jù)庫(kù)名稱(chēng)');
同樣是過(guò)程方式,mysqli有一點(diǎn)和mysql的用法不同的是,:
mysql函數(shù),很多函數(shù)都有2個(gè)參數(shù),一個(gè)是
sql語(yǔ)句等, 一個(gè)是mysql連接標(biāo)識(shí), 而這個(gè)mysql連接標(biāo)識(shí)參數(shù)是可以省略的!
但是mysqli兩個(gè)參數(shù)必須, 而且順序相反, 比如:
1
2
3
4
5
mysql_query('show tables', $Con );
//也可以省略第二個(gè)參數(shù),寫(xiě)成:
mysql_query('show tables');
//但是mysqli不行
mysqli_query( $Con, 'show tables');
其他回答
下個(gè)php手冊(cè),里面有示例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();