PHP連接數(shù)據(jù)庫之PHP連接MYSQL數(shù)據(jù)庫代碼
創(chuàng)新互聯(lián)專注于鑲黃企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,商城系統(tǒng)網(wǎng)站開發(fā)。鑲黃網(wǎng)站建設(shè)公司,為鑲黃等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
?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
reg.php文件
?php
header("Content-type:text/html;charset=utf-8");
if($_POST){
$dsn = 'mysql:dbname=1104javab;host=127.0.0.1';
$user = 'root';
$password = '';
try{
$pdo = new pdo($dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND = 'SET NAMES \'UTF8\'')
);
}catch(Exception $e){
echo '錯誤'.$e-getmessage();
}
$name = $_POST['name'];
$pwd = md5($_POST['pwd']);
$sql = "insert into 表 (username,password) values ('$name','$pwd')";
$exec = $pdo-query($sql);
if($exec){
echo "scriptalert('成功');location.href='reg.html'/script";
}else{
echo "scriptalert('失敗');location.href='reg.html'/script";
}
}
?
reg.html文件
form action='reg.php' method='post'
用戶名:input type='text' name='name'
密碼:input type='password' name='pwd'
input type='submit' value='submit'
/form
login.html文件
form action='reg.php' method='post'
用戶名:input type='text' name='name'
密碼:input type='password' name='pwd'
input type='submit' value='submit'
/form
login.php文件
header("Content-type:text/html;charset=utf-8");
if($_POST){
$dsn = 'mysql:dbname=1104javab;host=127.0.0.1';
$user = 'root';
$password = '';
try{
$pdo = new pdo($dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND = 'SET NAMES \'UTF8\'')
);
}catch(Exception $e){
echo '錯誤'.$e-getmessage();
}
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$sql = "select user_id from 表名 where username='$name' and password='$pwd'";
$stmt = $pdo-query($sql);
$info = $stmt-fetch(PDO::FETCH_ASSOC);
if($info){
echo "登錄成功";
}else{
echo "登錄失敗";
}
}
大概這樣
創(chuàng)建conn.php,連接數(shù)據(jù)庫。
$dns = 'mysql:host=127.0.0.1;dbname=test';
$username = 'root';
$password = 'root';
// 1.連接數(shù)據(jù)庫,創(chuàng)建PDO對象
$pdo = new PDO($dns,$username,$password);
創(chuàng)建login.html,登陸頁面。
用戶名
密 碼
創(chuàng)建login.php,驗證賬號密碼。
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("錯誤執(zhí)行");
}//檢測是否有submit操作
include('conn.php');//鏈接數(shù)據(jù)庫
$name = $_POST['name'];//post獲得用戶名表單值
$pwd = sha1($_POST['password']);//post獲得用戶密碼單值
if ($name $pwd){//如果用戶名和密碼都不為空
$sql = "select * from user where username = '$name' and password='$pwd'";//檢測數(shù)據(jù)庫是否有對應(yīng)的username和password的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
if($stmt-fetch(PDO::FETCH_BOUND)){//0 false 1 true
header("refresh:0;url=welcome.html");//如果成功跳轉(zhuǎn)至welcome.html頁面
exit;
}else{
echo "用戶名或密碼錯誤";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫不完整";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";
//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
$pdo = null;
創(chuàng)建signup.html,注冊頁面
用戶名:
密 碼:
創(chuàng)建signup.php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("錯誤執(zhí)行");
}//判斷是否有submit操作
$name=$_POST['name'];//post獲取表單里的name
$pwd = sha1($_POST['password']);//post獲取表單里的password
include('conn.php');//鏈接數(shù)據(jù)庫
$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向數(shù)據(jù)庫插入表單傳來的值的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
$stmt-fetch(PDO::FETCH_BOUND);
if (!$stmt){
die('Error: ' . $stmt-getMessage());//如果sql執(zhí)行失敗輸出錯誤
}else{
echo "注冊成功";//成功輸出注冊成功
}
$pdo = null;//關(guān)閉數(shù)據(jù)庫
u_signup.htmhtml
head
/head
title歡迎注冊網(wǎng)絡(luò)圖書銷售信息管理系統(tǒng)/title
body
p align="center" class="style"會員注冊/p
hr
form name="form1" method="post" action="u_signup.php"
p align="center"身份證號input name="u_sfzh" type="text" maxlength="18"/p
p align="center"會員姓名input name="u_hyxm" type="text" /p
p align="center"會員密碼input name="u_hymm1" type="password" /p
p align="center"密碼確認(rèn)input name="u_hymm2" type="password" /p
p align="center"聯(lián)系電話input name="u_lxdh" type="text" /p
p align="center"聯(lián)系地址input name="u_lxdz" type="text" /p
p align="center"銀行名稱input name="u_yhmc" type="text" /p
p align="center"銀行卡號input name="u_yhkh" type="text" /pp align="center"input name="u_return" type="submit" value="會員注冊"/p
/form
/body
/html u_signup.php?php
//驗證身份證號
$sfzh=$_POST['u_sfzh'];
if(empty($sfzh))
die("身份證號不能為空");
else if(strlen($sfzh)!=18)
die("身份證號應(yīng)為18位");
else if(!is_numeric($sfzh))
die("身份證號應(yīng)為18位數(shù)字");//驗證會員姓名
$hyxm=$_POST['u_hyxm'];
if(empty($hyxm))
die("會員姓名不能為空");
else if(strlen($hyxm)4)
die("會員姓名應(yīng)最少2個字符");
//驗證會員密碼
$hymm1=$_POST['u_hymm1'];
$hymm2=$_POST['u_hymm2'];
if(empty($hymm1) or empty($hymm2))
die("會員密碼不能為空");
else if(strlen($hymm1)4 or strlen($hymm2)4 )
die("會員密碼至少是4個字符");
else if($hymm1!=$hymm2)
die("兩次輸入的密碼不一致");//驗證聯(lián)系電話
$lxdh=$_POST['u_lxdh'];
if(empty($lxdh))
die("聯(lián)系電話不能為空");
else if(!is_numeric($lxdh))
die("聯(lián)系電話應(yīng)為數(shù)字");//驗證聯(lián)系地址
$lxdz=$_POST['u_lxdz'];
if(empty($lxdz))
die("聯(lián)系地址不能為空");
else if(strlen($lxdz)6)
die("聯(lián)系地址應(yīng)最少6個字符");//驗證銀行名稱和銀行卡號
$yhmc=$_POST['u_yhmc'];
$yhkh=$_POST['u_yhkh'];
if(empty($yhmc) or empty($yhkh))
die("銀行名稱和銀行卡號不能為空");
else if(strlen($yhmc)4 or strlen($yhkh)4 )
die("銀行名稱和銀行卡號至少是4個字符");//獲取系統(tǒng)日期和時間
$v_time=date("Y-m-d H:i:s");include "conn.php";mysql_query("set names 'GB2312'");//判斷是否存在相同用戶
//查詢數(shù)據(jù)的sql語句形式:select * from member where 身份證號='123456789012345678'
$v_find="select * from member where 身份證號='".$sfzh."'";
//echo($v_find);
//echo("br");
$result=mysql_query($v_find,$conn);
$record=mysql_num_rows($result);
if($record0)
die($sfzh."該會員身份證號已經(jīng)注冊,不得重復(fù),注冊失敗。");//注冊用戶
//插入數(shù)據(jù)的sql語句形式:insert into member values('11111111111111','1111',''...)
$v_insert="insert into member values('".$sfzh."','".$hyxm."','".$hymm1."','".$lxdh."','".$lxdz."','".$yhmc."','".$yhkh."','".$v_time."',0)";//echo($v_insert);
//echo("br");$result=mysql_query($v_insert);echo("注冊成功");?conn.php?php
$host="localhost";
$user="root";
$password="123456";$conn=mysql_connect($host,$user,$password);
if(!$conn)
die("連接MySQL服務(wù)器失敗。".mysql_error());$dbase="bookstore";
$db_select=mysql_select_db($dbase,$conn);
if(!$db_select)
die("連接MySQL數(shù)據(jù)庫失敗。".mysql_error());
?