PHP MySQL Classic database connection class Code _php tutorial

Source: Internet
Author: User
Tags php mysql
function MySQL Tutorial connector (): Constructors for classes, defining and containing configuration information
function Connectmysql (): Open Database Tutorial Connection
function Close (): Close database connection
function Returnsql ($sql): Executes a statement that returns an array of rows
function ExecuteSQL ($sql): Executes a query to return success
function Returndb ($sql): Execute query, return data set
function Selectlimit ($sql, $offset _b, $offset _n=0)
: Paging query, returning dataset parameters (SQL statement, start position, number of rows read)
function Datearray ($sql, $startid, $endid)
: Paged query, returns two-dimensional array parameters (SQL statement, start position, read row count)
function GetArray ($sql): Executes a query of two fields, returns an array of format array[row["0"]]=>row["1"]


*/

Class Mysqlconnector
{
/* Public: Database connection parameters */
var $dbhost; Server address
var $dbname; Database name
var $dbusername; Connect account
var $dbpassword; Connection password
var $setnames; Database encoding

function Mysqlconnector ()//constructor, Database link configuration
{
$this->dbname = "Xixia";
$this->dbhost = "localhost";
$this->dbusername = "root";
$this->dbpassword = "123456";
$this->setnames= "GBK";
}

function Connectmysql ()//Link database, return active connection
{
$openconn = Mysql_pconnect ($this->dbhost, $this->dbusername, $this->dbpassword) or Die ("Connect database error, check configuration!");
mysql_query ("Set Names"). $this->setnames. "'", $openconn);
mysql_select_db ($this->dbname, $openconn);
return $openconn;
}

/*
*
* Executes a query statement that returns an array of rows
*/

function Returnsql ($sql)
{
$array _result= "";

Mysql_unbuffered_query
$db _result=mysql_query ($sql, $this->connectmysql ());
if ($db _result) {
$array _result=mysql_fetch_array ($db _result);
}
Mysql_free_result ($db _result); Release recordset
return $array _result;

}

/*
*
* Execute Query statement, return data
*
*/

function Returndb ($sql)
{
$db _result=mysql_query ($sql, $this->connectmysql ());
return $db _result;

}

/*
*
* Executes the query statement, returns an array of two columns, primarily for the drop-down box, the previous column is values, and the next column is option
*
*/

function GetArray ($sql)
{
$array _result=array ();

$db _result=mysql_query ($sql, $this->connectmysql ());
if ($db _result) {
while ($row =mysql_fetch_row ($db _result))
{
$array _result[$row [0]]= $row [1];
}
}

return $array _result;

}

/*
*
* Executes an SQL statement that returns whether execution succeeded
*
*/

function ExecuteSQL ($sql)
{
$sql = Str_replace ("", "\", $sql);
$result =mysql_query ($sql, $this->connectmysql ());
if (! $result) {
echo " ";
return false;
}else{
return true;
}
}

/*

Reads the SQL statement in a page, returns a set of records,
Parameters are SQL statement, start line number, read number of bars (when passing 2, the number of rows is the number of read bars)
*/

function Selectlimit ($sql, $offset _b, $offset _n=0)
{

$result = "";
$this->checklink ($sql);
if (! $offset _n) {
$limit = "Limit". $offset _b;
}else{
$limit = "Limit". $offset _b. ",". $offset _n;
}
$sql. = $limit;
echo "
";
Echo $sql;

$result = $this->returndb ($sql);
return $result;
}

/*
*
* Convert datasets to an array
*
*/
function Datearray ($sql, $startid, $endid)
{
$array _result=array ();
$db _result= $this->selectlimit ($sql, $startid, $endid); reading datasets based on SQL statements

if ($db _result) {//DataSet exists
$i = 0;
while ($row =mysql_fetch_row ($db _result))//loop-fill Array
{
$array _result[$i]= $row;
$i + +;
}
}

return $array _result;

}

/*
*
* Close Link
*
*/
function Close ()
{
if ($this->linkid!=null)
{
Mysql_close ($this->linkid);
Unset ($this);
}
}

}
/*

* Use case:
$conn = new Mysqlconnector (); Instantiation of
$db = & $conn;

$db->returnsql ($sql)//Execute Query


*/

http://www.bkjia.com/PHPjc/630772.html www.bkjia.com true http://www.bkjia.com/PHPjc/630772.html techarticle function MySQL Tutorial connector (): Constructors for classes, defining and including configuration information function Connectmysql (): Open database Tutorial connect function close (): Closes the database connection fu...

  • 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.