php登陸注冊數(shù)據(jù)庫 php登錄注冊數(shù)據(jù)庫

PHP登陸注冊頁在本地測試時,出現(xiàn)連接數(shù)據(jù)庫失敗的情況怎么辦?

1、檢查環(huán)境正常

創(chuàng)新新互聯(lián),憑借十多年的成都網(wǎng)站制作、成都網(wǎng)站設(shè)計經(jīng)驗,本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計網(wǎng)站有成百上千案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)建站。

使用mysql -u root -p 可以進(jìn)入MySQL操作界面

直接使用/usr/local/php5/bin/php /web/test.php執(zhí)行可以連上數(shù)據(jù)庫

2、打開hosts加入

復(fù)制代碼代碼如下:127.0.0.1 qttc

使用qttc當(dāng)主機(jī)連接也正常,唯獨就不認(rèn)localhost。

3、localhost連接方式不同導(dǎo)致

為了了解PHP連接數(shù)據(jù)庫時,主機(jī)填寫localhost與其它的區(qū)別閱讀了大量資料,最后得知:

當(dāng)主機(jī)填寫為localhost時mysql會采用 unix domain socket連接

當(dāng)主機(jī)填寫為127.0.0.1時mysql會采用tcp方式連接

這是linux套接字網(wǎng)絡(luò)的特性,win平臺不會有這個問題

4、解決方法

在my.cnf的[mysql]區(qū)段里添加

復(fù)制代碼代碼如下:

protocol=tcp

保存重啟MySQL,問題解決!

php注冊和登錄用的是一個數(shù)據(jù)庫嗎

是的,注冊和登陸用的是一個數(shù)據(jù)庫。相當(dāng)于你在一個保險柜放一個東西,在別的保險柜是拿不到的。(中大型項目除外)

php+mysql怎么做登錄注冊

首先得到提交的數(shù)據(jù)

鏈接數(shù)據(jù)庫,查詢數(shù)據(jù)庫,查詢username 和pwd

提交的username 和 pwd? 跟數(shù)據(jù)庫查詢的username 和pwd做對比,

都相等那就是登陸成功

?php

mysql_connect('localhost','root','123');

mysql_select_db('lx');

mysql_query("SET?CHARACTER?SET?utf8");

mysql_query("SET?NAMES?utf8");

//數(shù)據(jù)庫lx?表user??字段id?username??pwd

//用md5加密,可以自己試試????

if(isset($_POST['user'])$_POST['tijiao']?==?'success'){

$query?=?mysql_query("select?pwd?from?user?where?username?=?'".$_POST['user']."'");

$num?=?mysql_num_rows($query);

if($num??0?){

while($info?=?mysql_fetch_array($query)){

if($info['pwd']?==?md5($_POST['pwd'])){

echo?'登陸成功';

}else{

echo?'登陸失敗';????

}

}

}else{

echo?'登陸失敗';

}

}

?

form?action=""?method="get"/

table?border="0"?cellspacing="0"?cellpadding="0"?

tr

td?class="fieldKey"?width="30%"用戶名:/td

td?class="fieldValue"?width="100%"input?type="text"?name="user"?//td

/tr

trtd?height="10"/td/tr

tr

td?class="fieldKey"密碼:/td

td?class="fieldValue"input?type="password"?name="pwd"?//td

/tr

/table

input?type="hidden"?name="tijiao"?value="success"?/

input?type="submit"?value="登陸"/

/form

php登錄頁面完整代碼連接數(shù)據(jù)庫

創(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ù)庫

本文名稱:php登陸注冊數(shù)據(jù)庫 php登錄注冊數(shù)據(jù)庫
網(wǎng)頁路徑:http://muchs.cn/article28/doesccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站改版、做網(wǎng)站網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)