新聞中心
PHP 判斷數(shù)據(jù)庫(kù)連接
$host='XXX';
網(wǎng)站設(shè)計(jì)制作過(guò)程拒絕使用模板建站;使用PHP+MYSQL原生開(kāi)發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺(tái)管理系統(tǒng);網(wǎng)站制作、成都網(wǎng)站建設(shè)收費(fèi)合理;免費(fèi)進(jìn)行網(wǎng)站備案等企業(yè)網(wǎng)站建設(shè)一條龍服務(wù).我們是一家持續(xù)穩(wěn)定運(yùn)營(yíng)了10年的成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司。
$user='XX';
$pass='XXX';
$db='edu';
$conn=@mysql_connect($host,$user,$pass);
if(!$conn){
echo 'script alert("數(shù)據(jù)庫(kù)錯(cuò)誤");window.location.href="XX.php";/script';
}
else{
mysql_select_db($db,$conn);
}
mysql_query("SET NAMES UTF8");
怎么將php與數(shù)據(jù)庫(kù)連接
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫(kù);
檢查php環(huán)境是否已開(kāi)啟mysql擴(kuò)展(一般情況下是開(kāi)啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒(méi)有mysql項(xiàng);b.打開(kāi)php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫(kù)主機(jī)地址
$host="localhost";
//定義mysql數(shù)據(jù)庫(kù)登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫(kù)登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫(kù)
$conn = mysql_connect($host,$user,$pwd);
//對(duì)連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫(kù)連接失敗!".mysql_errno());
}else{
echo "數(shù)據(jù)庫(kù)連接成功!";
}
?
php判斷數(shù)據(jù)庫(kù)是不是連接成功的測(cè)試?yán)?/h2>
form action="" method="post"
select name="host"
option value="localhost" selectedlocalhost/option
option value="127.0.0.1"127.0.0.1/option
/select
brbr
user:input type="text" name="user" value=""brbr
pwd :input type="passWord" name="pwd" value=""brbr
input type="submit" value="connent"
input type="reset" value="reset"
/form
?php
error_reporting(~E_ALL);
$host = $_POST['host'];
$user = $_POST['user'];
$pwd = $_POST['pwd'];
if(isset($_POST['host']) isset($_POST['user']) isset($_POST['pwd'])){
if(strlen($host)1 or strlen($user)1 or strlen($pwd)1){
echo "請(qǐng)完善相關(guān)數(shù)據(jù)庫(kù)鏈接信息。";
exit(0);
}
$conn = mysql_connect($host, $user, $pwd) or die("Error-數(shù)據(jù)庫(kù)連接失?。?);
if($conn){
echo "OK—數(shù)據(jù)庫(kù)連接成功!";
}
}
?
thinkphp 怎么 檢測(cè)是否跟數(shù)據(jù)庫(kù)連接
比如有個(gè)user的表,且表里有數(shù)據(jù)。
$user=M('User');
$list=$user-find();
dump($list);
若有數(shù)據(jù),則表示連接成功。土辦法。如果樓主還有不明白的,建議去后盾人找找相關(guān)教學(xué)視頻看看。
怎么測(cè)試android與php是否連接數(shù)據(jù)庫(kù)
參考下面代碼及代碼中的注釋即可:
PHP代碼:
conn.php是連接MySQL數(shù)據(jù)庫(kù)的。代碼如下:
?php
$dbhost = "localhost:3306";
$dbuser = "root"; //我的用戶名
$dbpass = ""; //我的密碼
$dbname = "testlogin"; //我的mysql庫(kù)名
$cn = mysql_connect($dbhost,$dbuser,$dbpass) or die("connect error");
@mysql_select_db($dbname)or die("db error");
mysql_query("set names 'UTF-8'");
?
login.php代碼:
?php
include ("conn.php");//連接數(shù)據(jù)庫(kù)
$username=str_replace(" ","",$_POST['name']);//接收客戶端發(fā)來(lái)的username;
$sql="select * from users where name='$username'";
$query=mysql_query($sql);
$rs = mysql_fetch_array($query);
if(is_array($rs)){
if($_POST['pwd']==$rs['password']){
echo "login succeed";
}else{
echo "error";
}
}
?
class LoginHandler implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//get username and password;
userName = user_name.getText().toString().trim();
password = pass_word.getText().toString().trim();
//連接到服務(wù)器的地址,我監(jiān)聽(tīng)的是8080端口
String connectURL="網(wǎng)站地址/text0/com.light.text/login.php/";
//填入用戶名密碼和連接地址
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//判斷返回值是否為true,若是的話就跳到主頁(yè)。
if(isLoginSucceed){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);
proDialog.dismiss();
}else{
proDialog.dismiss();
// Toast.makeText(ClientActivity.this, "登入錯(cuò)誤", Toast.LENGTH_LONG).show();
System.out.println("登入錯(cuò)誤");
}
}
}
//登入的方法,傳入用戶 密碼 和連接地址
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //用來(lái)取得返回的String;
boolean isLoginSucceed = false;
//test
System.out.println("username:"+userName);
System.out.println("password:"+password);
//發(fā)送post請(qǐng)求
HttpPost httpRequest = new HttpPost(connectUrl);
//Post運(yùn)作傳送變數(shù)必須用NameValuePair[]陣列儲(chǔ)存
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//發(fā)出HTTP請(qǐng)求
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若狀態(tài)碼為200則請(qǐng)求成功,取到返回?cái)?shù)據(jù)
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出字符串
result=EntityUtils.toString(httpResponse.getEntity());
ystem.out.println("result= "+result);
}
}catch(Exception e){
e.printStackTrace();
}
//判斷返回的數(shù)據(jù)是否為php中成功登入是輸出的
if(result.equals("login succeed")){
isLoginSucceed = true;
}
return isLoginSucceed;
}
當(dāng)前標(biāo)題:檢測(cè)php連接數(shù)據(jù)庫(kù) php7連接數(shù)據(jù)庫(kù)
網(wǎng)站路徑:http://fisionsoft.com.cn/article/dosdchs.html