Organize the template for native PHP connection to MySQL

Template for native PHP connection to MySQL:

<?php
header('Content-Type: text/html; charset=utf-8');

//Security Settings
if (!defined ('CHAOYI')){
	exit ( 'Access Defined!' );
}

/*Modify database link begin*/
$dbhost = '127.0.0.1';
$dbuser = 'root';
$ dbpwd = '123456';
$dbname = 'testDB';
/*Modify database link end*/

/* The following parts cannot be modified */
function sqlConnect(){
	global $conn;
	if(!$conn = @mysql_connect(DBHost,DBUser,DBPwd)){exit('Database connection failed');}
}
function sqlSetDB(){
	if(!mysql_select_db(DBName)){exit('The specified database could not be found');}
}
function sqlSetName(){
	if(!mysql_query('SET NAMES UTF8')){exit('Character set error');}
}
function sqlQuery($sql){
	if(!$result = mysql_query($sql)){exit('SQL execution failed');}
	return $result;
}
function sqlGetOne($sql){
	return mysql_fetch_array(sqlQuery($sql),MYSQL_ASSOC);
}
function sqlClose(){
	if(!mysql_close()){exit('close exception');}
}

define('DBHost',$dbhost);
define('DBUser',$dbuser);
define('DBPwd',$dbpwd);
define('DBName',$dbname);

sqlConnect();
sqlSetDB();
sqlSetName();

 

Test code:

<?php
define('CHAOYI',true);
require "connect.inc.php";
$str = '<>Who to read and ask Jun to chant, "The water falls and the fragrance floats";
$str2 = htmlspecialchars(trim($str), ENT_QUOTES);
echo $str2;
$sql = "SELECT id,keyword FROM tb_user WHERE telephone='' LIMIT 1";
$result = sqlGetOne($sql);
print_r($result);
sqlClose();

 

Effect picture:

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691683&siteId=291194637