?php
創(chuàng)新互聯(lián)公司主營五峰網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),五峰h5微信平臺小程序開發(fā)搭建,五峰網(wǎng)站營銷推廣歡迎五峰等地區(qū)企業(yè)咨詢
$conn
=
mysql_connect('localhost','root','')
or
die('連接數(shù)據(jù)庫失敗,請檢查您的數(shù)據(jù)庫配置');
?
mysql_connect第二個參數(shù)是用戶名,第三個參數(shù)
默認(rèn)情況下,用上面測試下,如果不行就看下面是密碼
?php
$conn
=
mysql_connect('localhost','用戶名','密碼')
or
die('連接數(shù)據(jù)庫失敗,請檢查您的數(shù)據(jù)庫配置');
?
參考下面代碼及代碼中的注釋即可:
PHP代碼:
conn.php是連接MySQL數(shù)據(jù)庫的。代碼如下:
?php
$dbhost = "localhost:3306";
$dbuser = "root"; //我的用戶名
$dbpass = ""; //我的密碼
$dbname = "testlogin"; //我的mysql庫名
$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ù)庫
$username=str_replace(" ","",$_POST['name']);//接收客戶端發(fā)來的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)聽的是8080端口
String connectURL="網(wǎng)站地址/text0/com.light.text/login.php/";
//填入用戶名密碼和連接地址
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//判斷返回值是否為true,若是的話就跳到主頁。
if(isLoginSucceed){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);
proDialog.dismiss();
}else{
proDialog.dismiss();
// Toast.makeText(ClientActivity.this, "登入錯誤", Toast.LENGTH_LONG).show();
System.out.println("登入錯誤");
}
}
}
//登入的方法,傳入用戶 密碼 和連接地址
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //用來取得返回的String;
boolean isLoginSucceed = false;
//test
System.out.println("username:"+userName);
System.out.println("password:"+password);
//發(fā)送post請求
HttpPost httpRequest = new HttpPost(connectUrl);
//Post運(yùn)作傳送變數(shù)必須用NameValuePair[]陣列儲存
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//發(fā)出HTTP請求
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若狀態(tài)碼為200則請求成功,取到返回數(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;
}
比如有個user的表,且表里有數(shù)據(jù)。
$user=M('User');
$list=$user-find();
dump($list);
若有數(shù)據(jù),則表示連接成功。土辦法。
數(shù)據(jù)庫有很多種類:mysql,oracle,mssql,db2等等。PHP操作數(shù)據(jù)庫的時候,要保證該類型數(shù)據(jù)庫的擴(kuò)展已開啟。這里連接的數(shù)據(jù)庫以mysql為例:
?php
//數(shù)據(jù)庫服務(wù)器地址
$host="localhost";?
//連接數(shù)據(jù)庫用戶名
$uname="root";?
//連接數(shù)據(jù)庫密碼
$upass="";?
//連接數(shù)據(jù)庫
$conn=mysql_connect($host,?$uname,$upass);
//判斷連接
if(!$conn){
die("連接數(shù)據(jù)庫失??!").mysql_errno();????
}
//連接成功,其他操作省略
?
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 "請完善相關(guān)數(shù)據(jù)庫鏈接信息。";
exit(0);
}
$conn = mysql_connect($host, $user, $pwd) or die("Error-數(shù)據(jù)庫連接失敗!");
if($conn){
echo "OK—數(shù)據(jù)庫連接成功!";
}
}
?
1、檢查環(huá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ī)連接也正常,唯獨(dú)就不認(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,問題解決!
網(wǎng)站標(biāo)題:測試數(shù)據(jù)庫連接php 測試數(shù)據(jù)庫連接語句
新聞來源:http://m.rwnh.cn/article2/doopcic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、響應(yīng)式網(wǎng)站、微信小程序、企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)