php鏈接mysql必備條件:
創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元波密做網(wǎng)站,已為上家服務(wù),為波密各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
已安裝mysql數(shù)據(jù)庫;
檢查php環(huán)境是否已開啟mysql擴展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫主機地址
$host="localhost";
//定義mysql數(shù)據(jù)庫登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫
$conn = mysql_connect($host,$user,$pwd);
//對連接進行判斷
if(!$conn){
die("數(shù)據(jù)庫連接失?。?.mysql_errno());
}else{
echo "數(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)成功建立!";
創(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ù)庫
您好,寫一個專門用于連接數(shù)據(jù)庫的文件:db_conn.php 如下:
?php
define("DBSERVER","localhost");
define("USER","root");
define("PASSWORD","your password");
define("DB","dbName");
function connectMySQL()
{
@mysql_pconnect(DBSERVER,USER,PASSWORD) or die("服務(wù)器繁忙,請刷新后再嘗試建立連接");
@mysql_select_db(DB) or die("數(shù)據(jù)庫正在連接中。。。");
}
connectMySQL();
mysql_query("set names utf8");
?
然后在需要使用到連接數(shù)據(jù)庫的網(wǎng)頁加入:
require("db_conn.php");
你寫的這個只是數(shù)據(jù)庫連接的代碼,你只是連接了數(shù)據(jù)庫,可以對你的“”數(shù)據(jù)庫進行"CURD"操作,$conn返回的是resource,mysql_select_db()和
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ù)庫連接標識;
private
$sql;
//sql執(zhí)行的語句
private
$result;
//query的資源標識符
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è)置編碼失敗!');
}
}
}
}
當前標題:php網(wǎng)頁連接到數(shù)據(jù)庫 php網(wǎng)頁連接到數(shù)據(jù)庫失敗
網(wǎng)站網(wǎng)址:http://muchs.cn/article14/hggode.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、網(wǎng)站營銷、外貿(mào)網(wǎng)站建設(shè)、、品牌網(wǎng)站制作、軟件開發(fā)
聲明:本網(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)