Package for database operations in PHP

xiaoxiao2021-03-06  41

Many of the dynamic mesh design should involve the operation of the database, but sometimes you need a large number of modifications to use other background databases with the need. This is a boring, time-consuming and easy error. In fact, we can use the classes in PHP to implement the package of database operations, so that the procedures written can complete the changes to the background database under small changes.

Now we package it in dbfz.inc and its design is as follows:

Class dbinterface {var $ dbId = 1; // Used to determine the currently operated database, when DBID is 1 represents MySQL, when 2 represents SQL Server, ODBC or other.

Var $ dbhost; // The host domain where the database is located

Var $ dbusername; // Database Username

Var $ dbpassword; // user password

// Set the host, username and password function

Function SetParameter ($ HOST, $ Username, $ Password) {

$ this-> dbusername = $ usrname;

$ this-> dbhost = $ host;

$ this-> dbpassword = $ password;

} // Joint log function

Function dbconnect () {

Switch ($ this-> dbid)

{

Case 1;

Return @MYSQL_Connect ($ this-> Dbhost, $ this-> dbusername, $ this-> dbpassword);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

// Close the library function

Function dbclose ($ dataHandle) {

Switch ($ this-> dbid)

{

Case 1;

MySQL_Close ($ dataHandle);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

/ / Execute SQL statement functions

Function DBQuery ($ DBNAME, $ SQL, $ DBHANDLE) {

Switch ($ this-> dbid)

{

Case 1;

Return @MYSQL_DB_QUERY ($ DBNAME, $ SQL, $ DBHANDLE);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

// Retrieve the current record function of the SQL return value

Function DBFETCHROW ($ DataHandle, $ OFFSET = -1) {

Switch ($ this-> dbid)

{

Case 1;

@MYSQL_DATA_SEEK ($ DataHandle, $ OFFSET);

Return @mysql_fetch_row ($ dataHandle);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

/ / Return to the retrieval record number function

Function DBNUMROWS ($ DataHandle) {

Switch ($ this-> dbid)

{

Case 1;

Return @mysql_num_rows ($ dataHandle);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

// Return to the search column function

Function dbnumcols ($ dataHandle) {

Switch ($ this-> dbid)

{

Case 1;

Return @mysql_num_fields ($ dataHandle);

Case 2;

// Functions that support SQL Server

Case 3;

// Function that supports ODBC

}

}

}

The instructions now use are as follows:

In the program, use the dbinterface class to born a target $ test = new dbinterface;

Setting parameters

TEST -> $ dbusername; user name

TEST -> $ dbpassword; password

TEST -> $ dbhost; host

Void setParameter (String Host, String Username, String Password);

Database connection: dbhandle test-> dbconnect ();

Return Value: FASLE, Database Connection Error

> 0, database connection handle

Database Close: Void Test-> DBClose (DBHANDLE);

Table Operation: int Test-> DBQuery (String DatabaseName, String SQL, DBHANDLE); Execute SQL statement

Return value: false, SQL execution error

> 0, SQL is correct, pointing to SQL return value,

Data Operation: int Test-> DBFETCHROW (DataHandle, Int Offset); retrieves the current record of the SQL return value, after successful execution, the pointer shifts the next record

INT TEST-> DBNUMROWS (DataHandle); Number of records obtained after obtaining SQL execution (primarily for SELECT statement)

INT TEST-> DBNUMCOLS (DataHandle); Number of recorded records obtained after obtaining SQL execution (primarily for SELECT statement)

Now let's send an example of explanation:

The database uses MQSQL: its host name "localhost", the username "root" and password "".

There is a TESTDB database in MySQL and table table1, including: Name and PAY two fields

-----

test </ title></p> <p></ HEAD> <body></p> <p><P> <? PHP</p> <p>Require ("testdb.inc"); // Load DBINTERFACE class</p> <p>$ test = new dbinterface; // Generate an object with class DbinterFace</p> <p>$ TEST-> SetParameter ("localhost", "root", ""); // set database parameters</p> <p>$ DB = $ TEST-> dbconnect (); // Connect the database</p> <p>$ Query = "SELECT NAME, PAY"; // Setting SQL statement</p> <p>$ TEMP_RESULT = $ TEST-> DBQuery ("TestDB", $ Query, $ dB); // Execute Data Library Operation</p> <p>echo "</p> <p>"</p> <p>$ ls_num = $ test-> dbnumrows ($ TEMP_RESULT); // Number of records to obtain query results</p> <p>Echo $ LS_NUM;</p> <p>echo "</p> <p>"</p> <p>IF (ls_num> 0)</p> <p>{</p> <p>$ ls_col = $ test-> dbnumcols ($ dB); // Number of columns to get the table</p> <p>Echo $ ls_col;</p> <p>echo "</p> <p>"</p> <p>$ CATE_RESULT = $ TEST-> DBFETCHROW ($ TEMP_RESULT, 0); // Take the first line of the number of records</p> <p>$ hcid = $ cate_result [0]; // Get Name value $ hcate = $ cate_result [1]; // Get the value of PAY</p> <p>Echo $ hcid;</p> <p>echo "</p> <p>"</p> <p>Echo $ HCATE;</p> <p>}</p> <p>?></p> <p><Hr></p> <p><Address> </ address></p> <p></ Body> </ html></p> <p>This is a simple application package to complete the operation of the database. If you want to operate other databases, you only need to modify the DBID variable in the DbinterFace class.</p> <p>Excerpt from: PHP Zhiyuan</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-112368.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="112368" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'W_2FayqobkfE8KUDU_2BgHQGiPSVUHaMzhOEWjDh_2BblZdn9g_2BOV_2Fs7xbiOB38PXAx_2Fo4tW4d2juGbw42VlLABhOCkA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>