。在学习的过程中Sqlhelper类中的连接总是不正确

WBOY
發布: 2016-06-13 11:08:10
原創
760 人瀏覽過

求助。在学习的过程中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();
}


------解决方案--------------------

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!