php連線資料庫

2021-08-19 03:29:33 字數 1469 閱讀 6357

新建乙個檔案connet.php

<?php

header('content-type:text/html;charset=utf-8');

define('db_host','localhost');

define('db_user','root');

define('db_pwd','guhaibin1314');

define('db_name','blog');//此處為在資料庫新建的資料庫名

$conn = @mysql_connet(db_host,db_user,db_pwd) or die('資料庫連線失敗:'.mysql_error()); //連線資料庫

@mysql_select_db(db_name) or die('資料庫連線錯誤'.mysql_error()); //連線對應的資料表

@mysql_query('set names utf8') or die('字符集錯誤'.mysql_error()); //設定字符集

?>

新建乙個data.php檔案,用來接收表單提交的資料,並把資料提交到資料庫

<?php

require 'connet.php';

$_birthday = $_post['year'].'-'.$_post['month'].'-'.$_post['day'];

$query = "insert into blog_user(user,pass,ques,ans,email,birthday,ps)

values('',sha1(''),'','','','','')";

//把獲取到的資料插入資料庫,此處對應你在資料庫新建的資料表項

mysql_query($query) or die('新增失敗'.mysql_error())';

echo mysql_affected_rows();//用於對ajax做返回值,此處的意思就是新增了幾行

mysql_close(); //關閉資料庫

?>

前台ajax表單提交資料

$.ajax(

}, async:true

})

判斷提交的使用者名稱是否重複

<?php

require'connet.php';

$query=mysql_query("select user from blog_user where user=''") or die('sql錯誤');

if(mysql_fetch_array($query,mysql_assoc)) //php中判斷乙個資料是否存在的方法mysql_fetch_array()

mysql_close();

?>

注意:php是特別嚴謹的語言,對於寫慣js鬆散型語言的程式媛來說,可能會忘了加分號。。注意一定一定別忘了加分號

php連線資料庫

create table message id tinyint 1 not null auto increment,user varchar 25 not null,title varchar 50 not null,content tinytext not null,lastdate date n...

php連線資料庫

天貓內部優惠券 設定資料庫變數 db host localhost 資料庫主機名稱,一般都為localhost db user root 資料庫使用者帳號,根據個人情況而定 db passw 資料庫使用者密碼,根據個人情況而定 db name test 資料庫具體名稱,以剛才建立的資料庫為準 連線資...

PHP連線資料庫

php連線資料庫函式 mysql connect 開啟mysql連線 mysql select db 開啟乙個資料庫 和or die 隱藏錯誤和條件顯示 mysql connect 主機 使用者名稱 密碼 mysql select db 開啟資料庫 鏈結標示符 如果不是特宣告連線標示符,則預設為是上...