。在學習的過程中Sqlhelper類中的串連總是不正確

來源:互聯網
上載者:User
求助。在學習的過程中Sqlhelper類中的串連總是不正確
學習寫一個登陸小程式。三個檔案SqlHelper.class.php,loginProcess.php,AdminService.class.php

我用的是zend開發工具。運行代碼的時候總是報:Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\empManage\SqlHelper.class.php on line 20

我猜想是類執行個體化之後建構函式的問題。
但是總是無法解決。希望有人幫我看下。非常感謝

SqlHelper.class.php

class SqlHelper{
//聲明類成員
public $conn;
public $dbname="emp";
public $username="root";
public $password="";
public $host="localhost";
//建構函式
public function _construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if (!$this->conn) {
die("串連失敗".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}

// 執行dql語句
public function excute_dql($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_error());
return $res;
}

//執行dml語句
public function excute_dml($sql){
$b=mysql_query($sql,$this->conn);
if (!$b){
return 0;
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示執行OK
} else{
return 2;//表示執行沒有行受到影響
}

}


}
//關閉資料庫連接
public function close_connect(){
if (!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>


AdminService.class.php:

require_once 'SqlHelper.class.php';
class AdminService{
public function checkAdmin($id,$password){
$sql="select * from admin where id=$id";
//建立一個SqlHelper對象
$sqlHelper=new SqlHelper();
$res=$sqlHelper->excute_dql($sql);//我把這個結果給你,你怎麼處理是你的事情
if ($row=mysql_fetch_assoc($res)){
if ($password==$row['password']){
return $row['name'];
}
}
//關閉資源
mysql_free_result($res);
$sqlHelper->close_connect();
//關閉串連
return "";
}
}
?>



loginProcess.php
require_once 'AdminService.class.php';
$id=$_POST['id'];
$password=$_POST['password'];

$adminService=new AdminService();
if($name=$adminService->checkAdmin($id,$password)){
header("Location:empMain.php?name=$name");
exit();
} else{
header("Location:login.php?errno=1");
exit();
}


------解決方案--------------------
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.