先mysql_connect()到mysql再做mysql_query()的相關(guān)操作。
在零陵等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、做網(wǎng)站 網(wǎng)站設(shè)計制作按需開發(fā)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站建設(shè),零陵網(wǎng)站建設(shè)費用合理。
====================================================================
?php
$server = "localhost";
$username = "root";
$password = "";
$database = "myabc";
$ranks = array(
1="newbie",
2="new member",
3="member",
4="high member",
5="very high member",
6="supreme member",
7="ultra member",
8="godlike member",
9="god member",
10="low god",
11="medium god",
12="high god",
13="very high god",
14="supreme god",
15="ultra god",
16="perfect"
);
$couldNotOpenDatabase = "Could not open databaseBR\n please check your settings in config.php";
$couldNotConnectMysql="Could not connect Mysql!";
$conn=mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
if (mysql_select_db($database,$conn))
{//數(shù)據(jù)庫存在,做相應(yīng)操作
}
else
{//數(shù)據(jù)庫不存在,創(chuàng)建一個,并做相應(yīng)操作
$query = "CREATE DATABASE $database";
$result = mysql_query($query);
mysql_select_db($database,$conn)or die ($couldNotOpenDatabase);
}
?
PHP連接數(shù)據(jù)庫之PHP連接MYSQL數(shù)據(jù)庫代碼
?php? ? $mysql_server_name= localhost ;? //改成自己的mysql數(shù)據(jù)庫服務(wù)器 ? $mysql_username= root ;? //改成自己的mysql數(shù)據(jù)庫用戶名 ? $mysql_password= ;? //改成自己的mysql數(shù)據(jù)庫密碼 ? $mysql_database= mycounter ; ?//改成自己的mysql數(shù)據(jù)庫名 ? $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( )?UNSIGNED?NOT?NULL? AUTO_INCREMENT? `count`?INT( )? UNSIGNED?NOT?NULL?DEFAULT? 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)成功建立!";? ? ?
PHP連接數(shù)據(jù)庫之PHP連接ACCESS數(shù)據(jù)庫代碼方法
? ? $conn?=?new?("ADODB Connection");? ? $connstr?=?"DRIVER={Microsoft?Access?Driver?(* mdb)};?DBQ=" ?realpath("data/db mdb");? ? $conn Open($connstr);? ? $rs?=?new?("ADODB RecordSet");? ? $rs Open("select?*?from?szd_t" $conn );? ? while(!?$rs eof)?{? ? $f?=?$rs Fields( );? ? echo?$f value;? ? $rs MoveNext();? ? }? ? ?
PHP連接數(shù)據(jù)庫之PHP連接MS SQL數(shù)據(jù)庫代碼方法
安裝SQL服務(wù)器并添加PHP的MSSQL擴(kuò)展
使用以下代碼連接并測試
?php? ? $myServer?=?localhost;?//主機(jī) ? $myUser?=?sa;?//用戶名 ? $myPass?=?password;?//密碼 ? $myDB?=?Northwind;?//MSSQL庫名 ? $s?=?@mssql_connect($myServer ?$myUser ?$myPass)? ? or?die(Couldnt?connect?to?SQL?Server?on?$myServer);? ? $d?=?@mssql_select_db($myDB ?$s)? ? or?die(Couldnt?open?database?$myDB);? ? $query?=?SELECT?TitleOfCourtesy+?+FirstName+?+LastName?AS?Employee?;? ? $query? =?FROM?Employees?;? ? $query? =?WHERECountry=USA?AND?Left(HomePhone ? )?=?( );? ? $result?=?mssql_query($query);? ? $numRows?=?mssql_num_rows($result);? ? echo? h ? ?$numRows? ?Row? ?($numRows?==? ???:?s)? ?Returned?/ h ;? ? while($row?=?mssql_fetch_array($result))? ? {? ? echo? li? ?$row[Employee]? ? /li;? ? }? ? ?
PHP連接數(shù)據(jù)庫之PHP連接Oracle數(shù)據(jù)庫
PHP提供了兩套函數(shù)與Oracle連接 分別是ORA_和OCI函數(shù) 其中ORA_函數(shù)略顯陳舊 OCI函數(shù)更新?lián)f更好一些 兩者的使用語法幾乎相差無幾 你的PHP安裝選項應(yīng)該可以支持兩者的使用
?? if?($conn=Ora_Logon("user@TNSNAME" "password"))? ? {?echo?"SUCCESS?!?Connected?to?databasen";? ? }else? ? {echo?"Failed?: (?Could?not?connect?to?databasen";}? ? Ora_Logoff($conn);? ? phpinfo();? ? ?? ? lishixinzhi/Article/program/PHP/201405/30761
你做好程序以后,把數(shù)據(jù)庫導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫
2、讀取這個sql文件里的sql語句,并執(zhí)行
3、生成一個數(shù)據(jù)庫連接參數(shù)的php文件
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
if?(mysql_query("CREATE?DATABASE?my_db",$con))
{
echo?"Database?created";
}
else
{
echo?"Error?creating?database:?"?.?mysql_error();
}
mysql_close($con);
?
?php
class?ReadSql?{
//數(shù)據(jù)庫連接
protected?$connect?=?null;
//數(shù)據(jù)庫對象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語句集
public?$sqlArr?=?array();
public?function?__construct($host,?$user,?$pw,?$db_name)?{
$host?=?empty($host)???C("DB_HOST")?:?$host;
$user?=?empty($user)???C("DB_USER")?:?$user;
$pw?=?empty($pw)???C("DB_PWD")?:?$pw;
$db_name?=?empty($db_name)???C("DB_NAME")?:?$db_name;
//連接數(shù)據(jù)庫
$this-connect?=?mysql_connect($host,?$user,?$pw)?or?die("Could?not?connect:?"?.?mysql_error());
$this-db?=?mysql_select_db($db_name,?$this-connect)?or?die("Yon?can?not?select?the?table:"?.?mysql_error());
}
//導(dǎo)入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打開文件錯誤");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
}
}
}
//獲取sql語句數(shù)組
public?function?GetSqlArr()?{
//去除注釋
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?創(chuàng)建數(shù)組
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
}?else?{
$this-sqlArr[]?=?$v;
}
}
}
//執(zhí)行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql語句錯誤:第"?.?$k?.?"行"?.?mysql_error());
}
}
return?true;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");
$rst?=?$sql-Import("./log_db.sql");
if?($rst)?{
echo?"Success!";
}
?