登录  /  注册
。在学习的过程中Sqlhelper类中的连接总是不正确
php中文网
发布: 2016-06-13 11:08:10
原创
567人浏览过

求助。在学习的过程中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中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学