Home > Backend Development > PHP Tutorial > php mysql classic database connection code_PHP tutorial

php mysql classic database connection code_PHP tutorial

WBOY
Release: 2016-07-13 17:05:36
Original
1036 people have browsed it

function mysql tutorial connector(): The constructor of the class, defines and contains configuration information
Function connectmysql() : Open database tutorial connection
function close() : Close the database connection
Function returnsql($sql) : Execute a statement and return an array of one row
Function executesql($sql): Execute a query and return whether it is successful
Function returndb($sql) : Execute the query and return the data set
Function selectlimit($sql,$offset_b,$offset_n=0)
: Paging query, return data set parameters (sql statement, starting position, number of rows read)
function datearray($sql,$startid,$endid)
                                                                                                 : Paginated query, return two-dimensional array Parameters (sql statement, starting position, number of rows read)
Function getarray($sql) : Execute a query on two fields and return an array, 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 to the database and return the active connection
{
            $openconn = mysql_pconnect($this->dbhost,$this->dbusername,$this->dbpassword) or die("Error connecting to database, please check the configuration!");
mysql_query("set names '".$this->setnames."'",$openconn);
mysql_select_db($this->dbname,$openconn);
          return $openconn;
}

/*
*
*Execute the query statement and return an array of a certain row
*/

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 the record set
return $array_result;
 
}

/*
*
*Execute the query statement and return data
*
*/

function returndb($sql)
{
$db_result=mysql_query($sql,$this->connectmysql());
Return $db_result;

}

/*
*
*Execute the query statement and return an array of two columns, mainly used for drop-down boxes. The former column is values ​​and the latter column is options
*
*/

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;
 
}

/*
*
*Execute a sql statement and return whether the execution is successful
*
*/

function executesql($sql)
{ 
//$sql = str_replace("","",$sql);
$result=mysql_query($sql,$this->connectmysql());
If(!$result){
echo "";
Return false;
}else{
Return true;
}
}

/*

Read the sql statement in pages and return the record set,
The parameters are the sql statement, the number of starting rows, and the number of items read (when passing the parameter 2, the number of starting lines is the number of items read)
*/

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 the data set into an array
*
*/
Function datearray($sql,$startid,$endid)
{
$array_result=array();
$db_result=$this->selectlimit($sql,$startid,$endid); $db_result=$this->selectlimit($sql,$startid,$endid); //Read the data set according to the sql statement
 
If($db_result){                                                                                                                                                        $i=0;
While($row=mysql_fetch_row($db_result)) //Loop to fill the 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
$db = &$conn;

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

*/

http://www.bkjia.com/PHPjc/630772.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630772.htmlTechArticlefunction mysql tutorial connector(): The constructor of the class, defines and contains configuration information function connectmysql(): Open the database Tutorial connection function close(): Close the database connection fu...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template