. The connection in the Sqlhelper class is always incorrect during learning.

Source: Internet
Author: User
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 --------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.