Php instance, php instance

Source: Internet
Author: User

Php instance, php instance

This example describes how to operate php-like object databases. Share it with you for your reference.

The specific implementation code is as follows:

Copy codeThe Code is as follows: // a database operation class is constructed here to encapsulate all database operations.
// It can be expanded to facilitate the use of background management programs
Class MySQLDB
{
Var $ host;
Var $ user;
Var $ passwd;
Var $ database;
Var $ conn;
 
// Use constructors to initialize Variables
// Connect to the database at the same time
Function MySQLDB ($ host, $ user, $ password, $ database)
{
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> passwd = $ password;
$ This-> database = $ database;
$ This-> conn = mysql_connect ($ this-> host, $ this-> user, $ this-> passwd) or
Die ("cocould not connect to $ this-> host ");
Mysql_select_db ($ this-> database, $ this-> conn) or
Die ("cocould not switch to database $ this-> database ");
}
 
// This function is used to close the database connection.
Function Close ()
{
MySQL_close ($ this-> conn );
}
 
// This function allows you to query databases.
Function Query ($ queryStr)
{
$ Res = Mysql_query ($ queryStr, $ this-> conn) or
Die ("cocould not query database ");
Return $ res;
}
 
// This function returns the record set
Function getRows ($ res)
{
$ Rowno = 0;
$ Rowno = MySQL_num_rows ($ res );
If ($ rowno> 0)
{
For ($ row = 0; $ row <$ rowno; $ row ++)
{
$ Rows [$ row] = MySQL_fetch_array ($ res );
// It is originally MySQL_fetch_row, but cannot be extracted as an array. It can only be indexed.
// This makes it easier to use indexes and names.
}
Return $ rows;
}
}
 
// Number of database records retrieved by this function
Function getRowsNum ($ res)
{
$ Rowno = 0;
$ Rowno = mysql_num_rows ($ res );
Return $ rowno;
}
 
// The number of fields returned by this function in the database table
Function getFieldsNum ($ res)
{
$ Fieldno = 0;
$ Fieldno = mysql_num_fields ($ res );
Return $ fieldno;
}
 
// This function returns the database table field name set.
Function getFields ($ res)
{
$ Fno = $ this-> getFieldsNum ($ res );
If ($ fno> 0)
{
For ($ I = 0; $ I <$ fno; $ I ++)
{
$ Fs [$ I] = MySQL_field_name ($ res, $ I); // obtain the name of the I Field
}
Return $ fs;
}
}
}
 
// Directly require the file during use and then instantiate it:
 
$ SqlDB = new MySQLDB ("localhost", "root", "root", "testdb ");
 
$ SQL = "select * from tableX ...";
 
$ Result = $ SqlDB-> Query ($ SQL); // Query
 
$ Rs = $ SqlDB-> getRows ($ result); // obtain the record set
 
$ Num = $ SqlDB-> getRowsNum ($ result); // obtain the number of records
 
... The remaining operation is the cyclic value,
 
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo ($ rs [$ I] ["field name"]);
}
 
...
Do not forget to close the data link.Copy codeThe Code is as follows: $ SqlDB-> Close (); of course this sentence can be left blank. php will automatically log out! But it is better to develop a good habit! And so on.

I hope this article will help you with PHP programming.

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.