十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
?php
成都創(chuàng)新互聯(lián)公司專注于瀘水網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供瀘水營銷型網(wǎng)站建設,瀘水網(wǎng)站制作、瀘水網(wǎng)頁設計、瀘水網(wǎng)站官網(wǎng)定制、成都微信小程序服務,打造瀘水網(wǎng)絡公司原創(chuàng)品牌,更為您提供瀘水網(wǎng)站排名全網(wǎng)營銷落地服務。
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo $sql;
mysql_close($conn);
echo "Hello!數(shù)據(jù)庫mycounter已經(jīng)成功建立!";
您好,寫一個專門用于連接數(shù)據(jù)庫的文件:db_conn.php 如下:
?php
define("DBSERVER","localhost");
define("USER","root");
define("PASSWORD","your password");
define("DB","dbName");
function connectMySQL()
{
@mysql_pconnect(DBSERVER,USER,PASSWORD) or die("服務器繁忙,請刷新后再嘗試建立連接");
@mysql_select_db(DB) or die("數(shù)據(jù)庫正在連接中。。。");
}
connectMySQL();
mysql_query("set names utf8");
?
然后在需要使用到連接數(shù)據(jù)庫的網(wǎng)頁加入:
require("db_conn.php");
你寫的這個只是數(shù)據(jù)庫連接的代碼,你只是連接了數(shù)據(jù)庫,可以對你的“”數(shù)據(jù)庫進行"CURD"操作,$conn返回的是resource,mysql_select_db()和
mysql_query()返回的則是布爾類型,所以在瀏覽器預覽的時候是沒有任何內(nèi)容的,有內(nèi)容也只是一個TRUE
連接數(shù)據(jù)庫的代碼如下:
數(shù)據(jù)庫操作類
class
mysql
{
private
$db_host;
//數(shù)據(jù)庫主機
private
$db_user;
//數(shù)據(jù)庫用戶名
private
$db_pwd;
//數(shù)據(jù)庫密碼
private
$db_database;
//數(shù)據(jù)庫名
private
$conn;
//數(shù)據(jù)庫連接標識;
private
$sql;
//sql執(zhí)行的語句
private
$result;
//query的資源標識符
private
$coding;
//數(shù)據(jù)庫編碼,gbk,utf8,gb2312
private
$show_error
=
true;
//本地調(diào)試使用,打印錯誤
public
function
__construct($db_host,
$db_user,
$db_pwd,
$db_database,
$coding)
{
$this-db_host
=
$db_host;
$this-db_user
=
$db_user;
$this-db_pwd
=
$db_pwd;
$this-db_database
=
$db_database;
$this-coding
=
$coding;
$this-connect();
}
private
function
connect()
{
$this-conn
=
@mysql_connect($this-db_host,
$this-db_user,
$this-db_pwd);
if
(!$this-conn)
{
//show_error開啟時,打印錯誤
if
($this-show_error)
{
$this-show_error('錯誤提示:鏈接數(shù)據(jù)庫失??!');
}
}
if
(!@mysql_select_db($this-db_database,
$this-conn))
{
//打開數(shù)據(jù)庫失敗
if
($this-show_error)
{
$this-show_error('錯誤提示:打開數(shù)據(jù)庫失?。?);
}
}
if
(!@mysql_query("set
names
$this-coding"))
{
//設置編碼失敗
if
($this-show_error)
{
$this-show_error('錯誤提示:設置編碼失??!');
}
}
}
}