求助。在學習的過程中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();
}
------解決方案--------------------