52Juanjuan's database class

zhaozj2021-02-12  180

The following is a public ODBC manipulation class that I have used in the SQL Server2000 in a PHP, I hope to give you a little bit of helping to begin PHP.

File: // Name and password are standard.

Define (c_db_host, $ host); define (c_db_name, $ dbname); define (c_db_user, $ dbuse); define (c_db_pass, $ dbpassword);

class Odbc {var $ Host = C_DB_HOST; // Hostname of our MySQL server var $ Database = C_DB_NAME; // Logical database name on that server var $ User = C_DB_USER; // Database user var $ Password = C_DB_PASS; // Database user's Password var $ useodbccursor = 0;

Var $ link_id = 0; // result of mysql_connect () var $ query_id = 0; // Result of most required mysql_Query () // var $ record = array (); // current mysql_fetch_array () - Result var $ row = 0; // Current Row Number Var $ Errno = 0; // Error State of Query Var $ Error = ""

Var $ auto_free = 0; ## set this to 1 to Automatically Free Results

Function Halt {Printf (" Database Error: % s
/ n", $ msg); Printf (" ODBC Error :% s (% s)
/ n ", $ this-> errno, $ this-> error); DIE (" session halted. ");}

Function connect () {if (0 == $ this-> link_id) {$ this-> link_id = ODBC_CONNECT ($ this-> Database, $ this-> User, $ this-> password, $ this-> users); IF ($ this-> link_id) {$ this-> hald ("link-id == false, odbc_connect failed");}}} function close ()}} Function Close () {ix (0! = $ this-> link_id) {ODBC_Close ($ THIS-> LINK_ID);}} Function Query ($ this-> connect (); # printf (" / n", $ query_string);

# REI@Netone.com.br suggester this ketbc_exec (). # He is on nt, connection # u mq m m m m - kk # $ this-> query_id = odbc_prepare ($ this- > Link_id, $ query_string); # $ this-> query_ok = odbc_execute ($ this-> query_id); $ this-> query_id = odbc_exec ($ this-> link_id, $ query_string); $ this-> row = 0; file : // odbc_binmode ($ this-> query_id, 1); file: // o o _ _ 4 ($ this-> query_id, 4096000); if (! $ this-> query_id) {$ this-> errno = 1; $ THIS- > Error = "General Error (The ODBC Interface Cannot Return DetaiD Error Messages)."; $ This-> Halt ("Invalid SQL:". $ Query_string);} Return $ this-> query_id;} Function nextRecord () {$ This-> Record = array (); $ this -> $ stat = odbc_fetch_into ($ this-> query_id, & $ this-> RECORD, $ this-> row); if (! $ this -> $ st) {IF ($ this-> auto_free) {odbc_free_result ($ this-> query_id); $ this-> query_id = 0;};} else {// add to record [] $ count = odbc_nu M_fields ($ this-> query_id); for ($ i = 1; $ i <$ count; $ i ) $ this-> record [strtolower (odbc_field_name ($ this-> query_id, $ i))] = StripsLashes $ this-> record [$ I - 1]);} Return $ this-> record;} Function Seek ($ POS) {$ this-> row = $ POS;} Function Metadata ($ TABLE) {$ count = 0 ; $ ID = 0; $ RES = array ();

$ this-> Connect (); $ ID = odbc_exec ($ this-> link_id, "select * form $ table"); if (! $ ID) {$ this-> errno = 1; $ this-> error = " ".". "; $ This-> hald (" metadata query, ");} $ count = ODBC_NUM_FIELDS ($ ID); for ($ I = 1; $ i <= $ count; $ 11) {$ RES [$ I] ["Table"] = $ table; $ name = odbc_field_name ($ ID, $ I); $ RES [$ I] ["name"] = $ name; $ RES [$ I] ["Type"] = ODBC_Field_Type ($ ID, $ Name); $ RES [$ I] ["LEN"] = 0; // Can We determine The Width of this Column? $ RES [$ I ] ["Flags"] = ""; // any optional flags to report?} ODBC_FREE_RESULT ($ ID); RETURN $ RES;} Function NumRows () {# Many ODBC Drivers Don n't support ODBC_NUM_ROWS () ON Select Statements. $ Num_Rows = ODBC_NUM_ROWS ($ this-> query_id); file: // printf ($ Num_Rows. "
");

# This is a workaround. It is intended to be ugly. IF ($ Num_Rows <0) {$ I = 10; While (ODBC_FETCH_ROW ($ this-> query_id, $ i)) $ i * = 10;

$ J = 0; While ($ I! = $ J) {$ K = $ J INTVAL (($ I- $ J) / 2); if (ODBC_FETCH_ROW ($ this-> query_id, $ k)) $ j = $ K; Else $ I = $ K; if ($ I- $ J) == 1) {if (odbc_fetch_row ($ this-> query_id, $ i)) $ j = $ i; Else $ I = $ j;}; file: // printf ("$ I $ j $ k
");}; $ num_rows = $ i;}

Return $ Num_Rows;}

function NumFields () {return count ($ this-> Record) / 2;} function FieldValue ($ Field_Name) {return odbc_result ($ this-> Query_ID, $ Field_Name); // Record [strtolower ($ Field_Name)];} Function AffectedRows () {RETURN ODBC_NUM_ROWS ($ this-> query_id);}}?>

My operation class is an ODBC interface class. The ODBC connection is first required before calling the data operation, and then pays the corresponding value to the following variables for manipulation:

$ Dbname = "localhost"; $ dbuser = "sa"; $ dbpassword = ""; ///

Assuming that the table Table1 in the database is performed as follows when the ODBC connection is successful.

$ db = new odbc ();

$ SQ1 = "Select * from $ TABLE"; // Select all records in the table

$ dB-> query ($ SQL);

While ($ result = $ db-> nextrecrod)

{

Echo $ Result;

}

$ dB-> close ();

转载请注明原文地址:https://www.9cbs.com/read-7331.html

New Post(0)