你寫的這個只是數(shù)據(jù)庫連接的代碼,你只是連接了數(shù)據(jù)庫,可以對你的“”數(shù)據(jù)庫進(jìn)行"CURD"操作,$conn返回的是resource,mysql_select_db()和

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、文水網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、商城網(wǎng)站定制開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為文水等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
mysql_query()返回的則是布爾類型,所以在瀏覽器預(yù)覽的時候是沒有任何內(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ù)庫連接標(biāo)識;
private
$sql;
//sql執(zhí)行的語句
private
$result;
//query的資源標(biāo)識符
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"))
{
//設(shè)置編碼失敗
if
($this-show_error)
{
$this-show_error('錯誤提示:設(shè)置編碼失敗!');
}
}
}
}
創(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
$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)成功建立!";
網(wǎng)頁標(biāo)題:php發(fā)布頁面鏈接數(shù)據(jù)庫 php7鏈接數(shù)據(jù)庫
當(dāng)前網(wǎng)址:http://chinadenli.net/article22/doojicc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站策劃、網(wǎng)站設(shè)計、靜態(tài)網(wǎng)站、Google、做網(wǎng)站
聲明:本網(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)