php mysql经典数据库连接类代码
function mysql教程connector() : 类的构造函数,定义和包含配置信息
function connectmysql() : 打开数据库教程连接
function close() : 关闭数据库连接
function returnsql($sql) : 执行一条语句,返回一行的数组
function executesql($sql) : 执行一段查询,返回是否成功
function returndb($sql) : 执行查询,返回数据集
function selectlimit($sql,$offset_b,$offset_n=0)
: 分页查询,返回数据集 参数(sql语句,开始位置,读取行数)
function datearray($sql,$startid,$endid)
: 分页查询,返回二维数组 参数(sql语句,开始位置,读取行数)
function getarray($sql) : 执行两个字段的查询,返回一个数组,格式 array[row["0"]]=>row["1"]
*/
class mysqlconnector
{
/* public: 数据库连接参数 */
var $dbhost; //服务器地址
var $dbname; //数据库名称
var $dbusername; //连接账号
var $dbpassword; //连接密码
var $setnames; //数据库编码function mysqlconnector() //构造函数,数据库链接配置
{
$this->dbname = "xixia";
$this->dbhost = "localhost";
$this->dbusername = "root";
$this->dbpassword = "123456";
$this->setnames="gbk";
}function connectmysql() //链接数据库,返回活动连接
{
$openconn = mysql_pconnect($this->dbhost,$this->dbusername,$this->dbpassword ) or die("连接数据库错误,请检查配置!");
mysql_query("set names '".$this->setnames."'",$openconn);
mysql_select_db($this->dbname,$openconn);
return $openconn;
}
/*
*
*执行查询语句,返回某一行的数组
*/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); //释放记录集
return $array_result;
}
/*
*
*执行查询语句,返回数据
*
*/
function returndb($sql)
{
$db_result=mysql_query($sql,$this->connectmysql());
return $db_result;
}
/*
*
*执行查询语句,返回某两列的数组,主要用于下拉框,前一列是values,后一列是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;
}/*
*
*执行一条sql语句,返回执行是否成功
*
*/function executesql($sql)
{
//$sql = str_replace("","\",$sql);
$result=mysql_query($sql,$this->connectmysql());
if(!$result){
echo "<!--出错了:" . $sql."-->";
return false;
}else{
return true;
}
}
/*
分页读取sql语句,返回纪录集,
参数分别为sql语句,开始行数,读取条数(传递2哥参数时,开始行数即为读取条数)
*/
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 "<br>";
// echo $sql;
$result = $this->returndb($sql);
return $result;
}
/*
*
*将数据集转化成数组
*
*/
function datearray($sql,$startid,$endid)
{
$array_result=array();
$db_result=$this->selectlimit($sql,$startid,$endid); //根据sql语句读取数据集
if($db_result){ //数据集存在
$i=0;
while($row=mysql_fetch_row($db_result)) //循环填充数组
{
$array_result[$i]=$row;
$i++;
}
}
return $array_result;
}
/*
*
*关闭链接
*
*/
function close()
{
if($this->linkid!=null)
{
mysql_close($this->linkid);
unset($this);
}
}
}
/**使用案例:
$conn= new mysqlconnector(); //实例化
$db = &$conn;
$db->returnsql($sql) //执行查询
*/
本文章提供了二款数据库教程连接实例,主要是讲php教程 mysql教程数据相关操作,有需要的朋友可以使用看看。
<?php
class mysql {
private $db_host; //主机地址
private $db_user; //用户名
private $db_pass; //连接密码
private $db_name; //名称
private $db_charset; //编码
private $conn;
public $debug=false;//调试开关,默认关闭
private $query_id; //用于判断sql语句是否执行成功
private $result; //结果集
private $num_rows; //结果集中行的数目,仅对select有效
private $insert_id; //上一步 insert 操作产生的 id
// 构造/析构函数
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 连接/选择数据库
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('数据库-连接失败:用户名或密码错误!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("数据库-选择失败:数据库 $this->db_name 不可用");
}
mysql_query("set names $this->db_charset");
return $this->conn;
}
// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("sql语句 <b>"$sql"</b> 执行时遇到错误");
return $this->query_id;
}
// 显示详细错误信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "错误:$msg <br/> 返回:$errinfo<p>";
}else{
echo '<p>出现错误!<p>';
}
}
// 获得query执行成功与否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查询所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 获取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 获取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 显示共有多少张表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}
// 显示共有多少个数据库
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有数据库 ".$this->num_rows($this->query_id)." 个<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[database]."<br />";
$i ++;
}
}
}
// 删除数据库:返回删除结果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默认删除当前数据库
$this->query("drop database $db_name");
}else {
$this->query("drop database $db_name");
}
if ($this->query_id) {
return "数据库 $db_name 删除成功";
}else {
$this->show_error("数据库 $db_name 删除失败");
}
}
// 删除数据表:返回删除结果
public function drop_table ($table_name) {
$this->query("drop table $table_name");
if ($this->query_id) {
return "数据表 $table_name 删除成功";
}else {
$this->show_error("数据表 $table_name 删除失败");
}
}
// 创建数据库
public function create_db ($db_name) {
$this->query("create database $db_name");
if($this->query_id){
return "数据库 $db_name 创建成功";
}else {
$this->show_error("数据库 $db_name 创建失败");
}
}
// 获取数据库版本
public function get_info(){
echo mysql_get_server_info();
}
// 释放内存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // end class
?>
下面提供一款自动选择数据库远程或本地连接代码
<?php
// 包含mysql操作类
include_once 'mysql.class.php';
// 本地mysql数据
$mysql_local_data = array('db_host'=>'localhost',
'db_user'=>'root',
'db_pass'=>'root',
'db_name'=>'test');
// 远程mysql数据
$mysql_remote_data = array('db_host'=>'61.183.41.178',
'db_user'=>'xxx',
'db_pass'=>'xxx',
'db_name'=>'xxx');
// 公用数据
$tb_prefix = 'php95_';
$db_charset = 'utf-8';
//本地连接成功则实例化本地mysql类,否则连接远程数据库并实例化mysql类
if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
$db = new mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
else
$db = new mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
$db->show_tables(); //测试:显示当前数据库下的所有表名
?>
假设我们要在test.php文件中操作虚拟主机的数据库,则首先要在本地调试,那么必然要连接本地、远程两个不同的数据库,问题:怎么让test.php自动识别当下该连接本地还是远程数据库呢?
强大的php可以利用mysql交互的相关函数可以获取数据表的字段信息,如可以获取数据表获取字段名,字段长度,字段信息等。$hostname="localhost"; //定义连接到的mysql服务器名
$username="root"; //定义用于连接的用户名
$password=""; //定义用于连接的密码
$link=mysql_connect($hostname,$username,$password); //打开mysql连接
$db_list=mysql_list_dbs($link); //列出数据库教程
$rows=mysql_num_rows($db_list); //取得返回结果数
$i=0;
while($i<$rows) //通过循环遍历结果集并赋值给对象
{
echo mysql_db_name($db_list,$i)."n"; //输出对象内容
echo "<p>n";
$i++;
}
mysql_close($link); //关闭mysql连接
//返回列的长度
$sql_str="select * from friends where id=1"; //定义sql语句
$result=mysql_query($sql_str); //执行sql语句
$re_a=mysql_fetch_array($result);
$re_len=mysql_fetch_lengths($result);
for($i=0;$i<count($re_len);$i++)
{
echo "返回结果的第".$i."列的长度为:".$re_len[$i];
echo "<p>";
}
mysql_close($link);
//获取字段信息
$result=mysql_query("select * from friends"); //执行sql查询
/*获取字段信息*/
$i=0;
while($i<mysql_num_fields($result)) //循环读取结果数
{
$i++;
echo "第".$i."列的信息:<br/>n";
$meta=mysql_fetch_field($result); //获取字段信息
if(!$meta) //如果值不存在
{
echo "no information available<br/>n"; //输出无可用信息
}
echo "<pre>
blob: $meta->blob
max_length: $meta->max_length
multiple_key: $meta->multiple_key
name: $meta->name
not_null: $meta->not_null
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
unsigned: $meta->unsigned
zerofill: $meta->zerofill
</pre>"; //结束去格式输出
}
//mysql_field_flags() 函数从结果中取得和指定字段关联的标志。
$re_field=mysql_field_flags($result,0);
$flag=explode(" ",$re_field);
print_r($flag);
$re_field=mysql_field_flags($result,1);
$flag=explode(" ",$re_field);
//列名
$result=mysql_query($sql_str); //执行sql语句
$re_name=mysql_field_name($result,0); //获取第一个字段的名称
echo "第一个字段的名称为:".$re_name;
echo "<p>";
$re_name=mysql_field_name($result,1); //获取第二个字段的名称
echo "第二个字段的名称为:".$re_name;
echo "<p>";
$re_name=mysql_field_name($result,2); //获取第三个字段的名称
echo "第三个字段的名称为:".$re_name;
echo "<p>";
$re_name=mysql_field_name($result,3); //获取第四个字段的名称
echo "第四个字段的名称为:".$re_name;
echo "<p>";
$re_name=mysql_field_name($result,4); //获取第五个字段的名称
echo "第五个字段的名称为:".$re_name;
echo "<p>";
class mysql {
private $db_host; //主机地址
private $db_user; //用户名
private $db_pass; //连接密码
private $db_name; //名称
private $db_charset; //编码private $conn;
private $query_id; //用于判断sql语句是否执行成功
private $result; //结果集
private $num_rows; //结果集中行的数目,仅对select有效
private $insert_id; //上一步 insert 操作产生的 id// 构造/析构函数
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}function __destruct () {
@mysql_close($this->conn);
}// 连接/选择数据库
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('数据库-连接失败:用户名或密码错误!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("数据库-选择失败:数据库 $this->db_name 不可用");
}
mysql_query("set names $this->db_charset");
return $this->conn;
}// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("sql语句 <b>"$sql"</b> 执行时遇到错误");
return $this->query_id;
}// 查询所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}// 获取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}// 获取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}// 显示共有多少张表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}// 显示共有多少个数据库
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有数据库 ".$this->num_rows($this->query_id)." 个<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[database]."<br />";
$i ++;
}
}
}// 删除数据库:返回删除结果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默认删除当前数据库
$this->query("drop database $db_name");
}else {
$this->query("drop database $db_name");
}
if ($this->query_id) {
return "数据库 $db_name 删除成功";
}else {
$this->show_error("数据库 $db_name 删除失败");
}
}// 删除数据表:返回删除结果
public function drop_table ($table_name) {
$this->query("drop table $table_name");
if ($this->query_id) {
return "数据表 $table_name 删除成功";
}else {
$this->show_error("数据表 $table_name 删除失败");
}}
// 创建数据库
public function create_db ($db_name) {
$this->query("create database $db_name");
if($this->query_id){
return "数据库 $db_name 创建成功";
}else {
$this->show_error("数据库 $db_name 创建失败");
}
}// 获取数据库版本
public function get_info(){
echo mysql_get_server_info();
}// 显示错误信息
public function show_error ($msg) {
$errinfo = mysql_error();
echo "错误:$msg <br/> 返回:$errinfo<p>";
}// 释放内存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}} // end class
odbc_connect() 函数用于连接到 ODBC 数据源。该函数有四个参数:数据源名、用户名、密码以及可选的指针类型参数。
。
odbc_exec() 函数用于执行 sql 语句
$db_user="dbuser"; //定义连接用户名
$db_pass="dbpass"; //连接用户对应的密码
$dsn="dsn"; //定义dsn资料来源
if(odbc_connect($dsn,$db_user,$db_pass)) //对odbc数据源进行连接
{
echo "成功连接到odbc数据源"; //如果成功输出内容
}
else
{
echo "连接到odbc时出现问题!"; //如果失败输出内容
}odbc_close_all(); //关闭所有打开的连接
echo "所有打开的odbc连接都已经被关闭!"; //执行函数后输出内容
看一个更高级的与主机连接
$db_host="server.mynetwork"; //定义主机名
$db_user="dbuser"; //定义连接用户名
$db_pass="dbpass"; //定义用户对应的密码
$dsn="dsn"; //定义dsn
$result=odbc_pconnect($dsn,$db_user,$db_pass); //打开持续有效的odbc连接
if($result) //对结果进行判断
{
echo "打开一个持续有效的连接"; //成功执行输出内容
}
echo "<br>";
if(odbc_close($result)) //尝试关闭连接
{
echo "关闭了?"; //如果成功关闭输出内容
}
else
{
echo "不能关!"; //如果关闭失败输出内容
}
查询数据库教程存
数据源进行连接
$my_sql="select * from usertable"; //定义sql语句
$result=odbc_do($myconn,$my_sql); //执行sql语句
echo odbc_num_rows($result);
echo "<table border="1">n";
echo "<tr>n";
echo "<td>id号</td>n";
echo "<td>name</td>n";
echo "<td>address</td>n";
echo "</tr>n";
while(odbc_fetch_row($result))
{
echo "<tr>n";
echo "<td>".odbc_result($result,1)."</td>n";
echo "<td>".odbc_result($result,2)."</td>n";
echo "<td>".odbc_result($result,3)."</td>n";
echo "</tr>n";
}
echo "</table>";
一些相关
odbc_free_result($result); //释放执行sql语句占用的内存
echo "已经成功释放结果集占用的内存!";
odbc_rollback($myconn)取消所有未提交的操作
odbc_commit($myconn) //提交所有未提交的操作
odbc_autocommit($myconn,false); //禁止自动commit
odbc_columnprivileges($myconn,"dbuser","管理员","usertable","name"); //列出给定表的列和权限
$result=odbc_columns($myconn); //列出指定表的列的名称
echo odbc_result_all($result);
相关文章
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
Antd-vue Table组件添加Click事件,实现点击某行数据教程
这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17- 这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
- 这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- 在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25