For help. In the course of learning, the connection in the Sqlhelper class always fails to learn to write a login applet. Three files: SqlHelper. class. php, loginProcess. php, and AdminService. class. php. I use zend Development tools. When running the code, always report: Warning: & nbsp; mysql_query (): help. The connection in the Sqlhelper class is always incorrect during learning.
Learn to write a login applet. Three files: SqlHelper. class. php, loginProcess. php, AdminService. class. php
I am using zend Development tools. When running the code, it always reports: Warning: mysql_query (): supplied argument is not a valid MySQL-Link resource in C: \ wamp \ www \ empManage \ SqlHelper. class. php on line 20
I guess it is the issue of constructing functions after class instantiation.
But it cannot be solved. Hope someone can help me. Thank you very much.
SqlHelper. class. php
Class SqlHelper {
// Declare a Class Member
Public $ conn;
Public $ dbname = "emp ";
Public $ username = "root ";
Public $ password = "";
Public $ host = "localhost ";
// Constructor
Public function _ construct (){
$ This-> conn = mysql_connect ($ this-> host, $ this-> username, $ this-> password );
If (! $ This-> conn ){
Die ("connection failed". mysql_error ());
}
Mysql_select_db ($ this-> dbname, $ this-> conn );
}
// Execute the dql statement
Public function excute_dql ($ SQL ){
$ Res = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ());
Return $ res;
}
// Execute the dml statement
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; // indicates that the execution is OK.
} Else {
Return 2; // indicates that no row is affected.
}
}
}
// Close the database connection
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 ";
// Create a SqlHelper object
$ SqlHelper = new SqlHelper ();
$ Res = $ sqlHelper-> excute_dql ($ SQL); // I will give you the result. how do you deal with it?
If ($ row = mysql_fetch_assoc ($ res )){
If ($ password = $ row ['password']) {
Return $ row ['name'];
}
}
// Close the resource
Mysql_free_result ($ res );
$ SqlHelper-> close_connect ();
// Close the connection
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 ();
}
------ Solution --------------------