MySQL數(shù)據(jù)庫(kù)函數(shù)詳解(2)

字號(hào):

3) int mysql_connect(string [hostname] [:port], string [username], string [password]);
    本函式建立與 MySQL 伺服器的連線。其中所有的參數(shù)都可省略。當(dāng)使用本函式卻不加任何參數(shù)時(shí),參
    數(shù) hostname 的內(nèi)定值為 localhost、參數(shù) username 的內(nèi)定值為 PHP 執(zhí)行行程的擁有者、參數(shù) password 則
    為空字串 (即沒(méi)有密碼)。而參數(shù) hostname 后面可以加冒號(hào)與埠號(hào),代表使用那個(gè)埠與 MySQL 連接。當(dāng)然在
    使用資料庫(kù)時(shí),早點(diǎn)使用 mysql_close() 將連線關(guān)掉可以節(jié)省資源。
    使用范例
    這是一位未具名網(wǎng)友提供的范例 (18-Feb-1999)
        $dbh = mysql_connect('localhost:3306','mcclain','standard');
    mysql_select_db('admreqs');
    $query = "insert into requests(date, request, email, priority,status) values
    (NOW(),'$description', '$email', '$priority', 'NEW')";
    $res = mysql_query($query, $dbh);
    $query = "select max(id) from requests";
    $res = mysql_query($query, $dbh);
    $err = mysql_error();
    if($err){
    echo "發(fā)生錯(cuò)誤,請(qǐng)通知站長(zhǎng)";
    }
    $row = mysql_fetch_row($res);
    echo "未來(lái)您使用的號(hào)碼為: ".$row[0];
    ?(4) int mysql_create_db(string db_name [, int link_id]);
    告訴由link_id標(biāo)識(shí)的MySQL服務(wù)器用給定的名稱來(lái)創(chuàng)建數(shù)據(jù)庫(kù)。如果數(shù)據(jù)庫(kù)創(chuàng)建成功,則返回真;如
    果出現(xiàn)錯(cuò)誤,則返回假。必須在數(shù)據(jù)庫(kù)有創(chuàng)建它的CREATE權(quán)限。
    可能利用mysql_query()較利用mysql_create_db()發(fā)布CREATE DATABASE 語(yǔ)句更為適合。
        $link=mysql_pconnect("localhost","sunsoft","suixiang"); or die("Could not connect");
    if(mysql_create_db("my_db"))
    print("Database created successfullyn");
    else
    print("Error creating database:%sn",mysql_error());
    ?>