1. 建立和关闭连接
1) mysql_connect()
resource mysql_connect([string hostname [:port][:/path/to/socket][,string username] [,string password]])
所有参数都是可选的
举例:
@mysql_connect(“localhost”, “user”, “password”)
or die(“Could not connect to mysql server!”);
注意,@符号表示禁止失败尝试导致的任何错误信息,用户将看到的是die()中指定的错误信息.
注意,当与多个mysql进行连接时,必须指定每个连接的链接ID,如下:
$link1 = @mysql_connect(“server1″, “user”, “password”)
or die(“Could not connect to mysql server!”);
$link2 = @mysql_connect(“server2″, “user”, “password”)
or die(“Could not connect to mysql server!”);
2) mysql_pconnect()
resource mysql_pconnect([string hostname [:port][:/path/to/socket][,string username] [,string password]])
与mysql_connect()不同的是:会首先查找现有链接,不存在时才创建.
注意,不需要显示关闭连接(mysql_close()),因为连接将放在池中,所以叫持久连接.
3) mysql_close()
boolean mysql_close([resource link_id])
关闭连接不是必须的,因为可以由mysql的垃圾回收来处理.
如果没有指定link_id,则关闭最近的链接.
2. 选择数据库
mysql_select_db()
boolean mysql_select_db(string db_name [, resource link_id])
3. 查询MySql
1) mysql_query()
resource mysql_query(string query [,resource link_id])
负责执行query.
2) mysql_db_query()
resource mysql_db_query(string database, string query [, resource link_id])
等价于mysql_select_db() + mysql_query(),从参数中就可以清楚的看出来.
4. 获取和显示数据
1) mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
从result_set 的指定row 中获取一个field 的数据. 简单但是效率低.
举例:
复制代码 代码如下:
$link1 = @mysql_connect(“server1″, “webuser”, “password”)
or die(“Could not connect to mysql server!”);
@mysql_select_db(“company”) or die(“Could not select database!”);
$query = “select id, name from product order by name”;
$result = mysql_query($query);
$id = mysql_result($result, 0, “id”);
$name = mysql_result($result, 0, “name”);
mysql_close();
复制代码 代码如下:
…
for ($i = 0; $i {
$id = mysql_result($result, 0, “id”);
$name = mysql_result($result, 0, “name”);
echo “Product: $name ($id)”;
}
…
复制代码 代码如下:
…
$query = “select id, name from product order by name”;
$result = mysql_query($query);
while(list($id, $name) = mysql_fetch_row($result)) {
echo “Product: $name ($id)”;
}
…
复制代码 代码如下:
…
$query = “select id, name from product order by name”;
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$name = $row['name'];//或者 $name = $row[1];
$name = $row['id'];//或者 $name = $row[0];
echo “Product: $name ($id)”;
}
…
复制代码 代码如下:
…
$query = “select id, name from product order by name”;
$result = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$name = $row->name;
$name = $row->id;
echo “Product: $name ($id)”;
}
…
复制代码 代码如下:
mysql_connect(“localhost”, “name”,”pwd”);
$dbs = mysql_list_dbs();
while (list($db) = mysql_fetch_row(dbs)) {
echo “$db
”;
}
复制代码 代码如下:
$conn = new COM(“ADODB.Connection”) or die(“Cannot start ADODB.Connection”);
$conn->Open(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\php5\\netBook.mdb”);
$rs = $conn->Execute(“select * from manage”); // 记录集
$num_columns = $rs->Fields->Count();
echo $num_columns . “
\n”;
for ($i=0; $i $fld[$i] = $rs->Fields($i);
}
$rowcount = 0;
while (!$rs->EOF) {
for ($i=0; $i {
echo htmlspecialchars($fld[$i]->value) . “\t”;
}
echo “
\n”;
$rowcount++; // rowcount 自增
$rs->MoveNext();
}
$rs->Close(); //关闭数据集
$conn->Close();
?>