导航:首页 > 编程语言 > mysqliphp教程

mysqliphp教程

发布时间:2022-04-24 03:15:05

php怎么用mysqli链接数据库和输出sql

一、mysql与mysqli的概念相关:
1、mysql与mysqli都是php方面的函数集,与
mysql数据库
关联不大。
2、在
php5
版本之前,一般是用php的
mysql函数
去驱动mysql数据库的,比如mysql_query()的函数,属于
面向过程
3、在php5版本以后,增加了mysqli的函数功能,某种意义上讲,它是mysql系统函数的增强版,更稳定更高效更安全,与mysql_query()对应的有mysqli_query(),属于面向对象,用对象的方式操作驱动mysql数据库
二、mysql与mysqli的区别:
1、mysql是非持继连接函数,mysql每次链接都会打开一个连接的进程。
2、mysqli是永远连接函数,mysqli多次运行mysqli将使用同一连接进程,从而减少了服务器的开销。mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。
三、mysql与mysqli的用法:
1:mysql(过程方式):
$conn
=
mysql_connect('
localhost
',
'user',
'password');//连接mysql数据库
mysql_select_db
('data_base');
//选择数据库$result
=
mysql_query('select
*
from
data_base');//第二个可选参数,指定打开的连接$row
=
mysql_fetch_row(
$result
)
)
//只取一行数据echo
$row[0];
//输出第一个字段的值
PS:mysqli以过程式的方式操作,有些函数必须指定资源,比如mysqli_query(资源标识,
SQL语句
),并且资源标识的参数是放在前面的,而mysql_query(SQL语句,'资源标识')的资源标识是可选的,默认值是上一个打开的连接或资源。
2、mysqli(对象方式):
$conn
=
new
mysqli('localhost',
'user',
'password','data_base');//要使用new
操作符
,最后一个参数是直接指定数据库//假如构造时候不指定,那下一句需要$conn
->
select_db('data_base')实现$result
=
$conn
->
query(
'select
*
from
data_base'
);$row
=
$result
->
fetch_row();
//取一行数据echo
row[0];
//输出第一个字段的值
使用new
mysqli('localhost',
usenamer',
'password',
'databasename');会报错,提示如下:
Fatal
error:
Class
'mysqli'
not
found
in
...
一般是mysqli是没有开启的,因为mysqli类不是
默认开启
的,win下要改php.ini,去掉php_mysqli.dll前的;,linux下要把mysqli编译进去。
四、mysql_connect()与mysqli_connect()
1.使用mysqli,可以把数据库名称当作参数传给mysqli_connect()函数,也可以传递给mysqli的
构造函数

2.如果调用mysqli_query()或mysqli的对象查询query()方法,则连接标识是必需的。

㈡ 如何使用php登录mysql,使用mysqli的登录方式,并插入一条数据,谁有有完整的php原代码

本文所述的是一个在PHP中以mysqli方式连接数据库的一个数据库类实例,该数据库类是从一个PHP的CMS中整理出来的,可实现PHP连接数据库类,MySQLi版,兼容PHP4,对于有针对性需要的朋友可根据此代码进行优化和修改。
?

<?php
#==================================================================================================
# Filename: /db/db_mysqli.php
# Note : 连接数据库类,MySQLi版
#==================================================================================================
#[类库sql]
class db_mysqli
{
var $query_count = 0;
var $host;
var $user;
var $pass;
var $data;
var $conn;
var $result;
var $prefix = "qinggan_";
//返回结果集类型,默认是数字+字符
var $rs_type = MYSQLI_ASSOC;
var $query_times = 0;#[查询时间]
var $conn_times = 0;#[连接数据库时间]
var $unbuffered = false;
//定义查询列表
var $querylist;
var $debug = false;
#[构造函数]
function __construct($config=array())
{
$this->host = $config['host'] ? $config['host'] : 'localhost';
$this->port = $config['port'] ? $config['port'] : '3306';
$this->user = $config['user'] ? $config['user'] : 'root';
$this->pass = $config['pass'] ? $config['pass'] : '';
$this->data = $config['data'] ? $config['data'] : '';
$this->debug = $config["debug"] ? $config["debug"] : false;
$this->prefix = $config['prefix'] ? $config['prefix'] : 'qinggan_';
if($this->data)
{
$ifconnect = $this->connect($this->data);
if(!$ifconnect)
{
$this->conn = false;
return false;
}
}
return true;
}
#[兼容PHP4]
function db_mysqli($config=array())
{
return $this->__construct($config);
}
#[连接数据库]
function connect($database="")
{
$start_time = $this->time_used();
if(!$this->port) $this->port = "3306";
$this->conn = @mysqli_connect($this->host,$this->user,$this->pass,"",$this->port) or false;
if(!$this->conn)
{
return false;
}
$version = $this->get_version();
if($version>"4.1")
{
mysqli_query($this->conn,"SET NAMES 'utf8'");
if($version>"5.0.1")
{
mysqli_query($this->conn,"SET sql_mode=''");
}
}
$end_time = $this->time_used();
$this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间]
$ifok = $this->select_db($database);
return $ifok ? true : false;
}
function select_db($data="")
{
$database = $data ? $data : $this->data;
if(!$database)
{
return false;
}
$this->data = $database;
$start_time = $this->time_used();
$ifok = mysqli_select_db($this->conn,$database);
if(!$ifok)
{
return false;
}
$end_time = $this->time_used();
$this->conn_times += round($end_time - $start_time,5);#[连接数据库的时间]
return true;
}
#[关闭数据库连接,当您使用持续连接时该功能失效]
function close()
{
if(is_resource($this->conn))
{
return mysqli_close($this->conn);
}
else
{
return true;
}
}
function __destruct()
{
return $this->close();
}
function set($name,$value)
{
if($name == "rs_type")
{
$value = strtolower($value) == "num" ? MYSQLI_NUM : MYSQLI_ASSOC;
}
$this->$name = $value;
}
function query($sql)
{
if(!is_resource($this->conn))
{
$this->connect();
}
else
{
if(!mysql_ping($this->conn))
{
$this->close();
$this->connect();
}
}
if($this->debug)
{
$sqlkey = md5($sql);
if($this->querylist)
{
$qlist = array_keys($this->querylist);
if(in_array($sqlkey,$qlist))
{
$count = $this->querylist[$sqlkey]["count"] + 1;
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>$count);
}else{
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
}
}
else{
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
}
}
$start_time = $this->time_used();
$func = $this->unbuffered && function_exists("mysqli_multi_query") ? "mysqli_multi_query" : "mysqli_query";
$this->result = @$func($this->conn,$sql);
$this->query_count++;
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查询时间]
if(!$this->result)
{
return false;
}
return $this->result;
}
function get_all($sql="",$primary="")
{
$result = $sql ? $this->query($sql) : $this->result;
if(!$result)
{
return false;
}
$start_time = $this->time_used();
$rs = array();
$is_rs = false;
while($rows = mysqli_fetch_array($result,$this->rs_type))
{
if($primary && $rows[$primary])
{
$rs[$rows[$primary]] = $rows;
}
else
{
$rs[] = $rows;
}
$is_rs = true;
}
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查询时间]
return ($is_rs ? $rs : false);
}
function get_one($sql="")
{
$start_time = $this->time_used();
$result = $sql ? $this->query($sql) : $this->result;
if(!$result)
{
return false;
}
$rows = mysqli_fetch_array($result,$this->rs_type);
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查询时间]
return $rows;
}
function insert_id($sql="")
{
if($sql)
{
$rs = $this->get_one($sql);
return $rs;
}
else
{
return mysqli_insert_id($this->conn);
}
}
function insert($sql)
{
$this->result = $this->query($sql);
$id = $this->insert_id();
return $id;
}
function all_array($table,$condition="",$orderby="")
{
if(!$table)
{
return false;
}
$table = $this->prefix.$table;
$sql = "SELECT * FROM ".$table;
if($condition && is_array($condition) && count($condition)>0)
{
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
}
if($orderby)
{
$sql .= " ORDER BY ".$orderby;
}
$rslist = $this->get_all($sql);
return $rslist;
}
function one_array($table,$condition="")
{
if(!$table)
{
return false;
}
$table = $this->prefix.$table;
$sql = "SELECT * FROM ".$table;
if($condition && is_array($condition) && count($condition)>0)
{
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
}
$rslist = $this->get_one($sql);
return $rslist;
}
//将数组写入数据中
function insert_array($data,$table,$insert_type="insert")
{
if(!$table || !is_array($data) || !$data)
{
return false;
}
$table = $this->prefix.$table;//自动增加表前缀
if($insert_type == "insert")
{
$sql = "INSERT INTO ".$table;
}
else
{
$sql = "REPLACE INTO ".$table;
}
$sql_fields = array();
$sql_val = array();
foreach($data AS $key=>$value)
{
$sql_fields[] = "`".$key."`";
$sql_val[] = "'".$value."'";
}
$sql.= "(".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")";
return $this->insert($sql);
}
//更新数据
function update_array($data,$table,$condition)
{
if(!$data || !$table || !$condition || !is_array($data) || !is_array($condition))
{
return false;
}
$table = $this->prefix.$table;//自动增加表前缀
$sql = "UPDATE ".$table." SET ";
$sql_fields = array();
foreach($data AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."'";
}
$sql.= implode(",",$sql_fields);
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
return $this->query($sql);
}
function count($sql="")
{
if($sql)
{
$this->rs_type = MYSQLI_NUM;
$this->query($sql);
$rs = $this->get_one();
$this->rs_type = MYSQLI_ASSOC;
return $rs[0];
}
else
{
return mysqli_num_rows($this->result);
}
}
function num_fields($sql="")
{
if($sql)
{
$this->query($sql);
}
return mysqli_num_fields($this->result);
}
function list_fields($table)
{
$rs = $this->get_all("SHOW COLUMNS FROM ".$table);
if(!$rs)
{
return false;
}
foreach($rs AS $key=>$value)
{
$rslist[] = $value["Field"];
}
return $rslist;
}
#[显示表名]
function list_tables()
{
$rs = $this->get_all("SHOW TABLES");
return $rs;
}
function table_name($table_list,$i)
{
return $table_list[$i];
}
function escape_string($char)
{
if(!$char)
{
return false;
}
return mysqli_escape_string($this->conn,$char);
}
function get_version()
{
return mysqli_get_server_info($this->conn);
}
function time_used()
{
$time = explode(" ",microtime());
$used_time = $time[0] + $time[1];
return $used_time;
}
//Mysql的查询时间
function conn_times()
{
return $this->conn_times + $this->query_times;
}
//MySQL查询资料
function conn_count()
{
return $this->query_count;
}
# 高效SQL生成查询,仅适合单表查询
function phpok_one($tbl,$condition="",$fields="*")
{
$sql = "SELECT ".$fields." FROM ".$this->db->prefix.$tbl;
if($condition)
{
$sql .= " WHERE ".$condition;
}
return $this->get_one($sql);
}
function debug()
{
if(!$this->querylist || !is_array($this->querylist) || count($this->querylist) < 1)
{
return false;
}
$html = '<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#CECECE"><tr><td>';
$html.= '<table cellpadding="1" cellspacing="1" width="100%">';
$html.= '<tr><th bgcolor="#EFEFEF" height="30px">SQL</th><th bgcolor="#EFEFEF" width="80px">查询</th></tr>';
foreach($this->querylist AS $key=>$value)
{
$html .= '<tr><td bgcolor="#FFFFFF"><div style="padding:3px;color:#6E6E6E;">'.$value['sql'].'</div></td>';
$html .= '<td align="center" bgcolor="#FFFFFF"><div style="padding:3px;color:#000000;">'.$value["count"].'</div></td></tr>';
}
$html.= "</table>";
$html.= "</td></tr></table>";
return $html;
}
function conn_status()
{
if(!$this->conn) return false;
return true;
}
}
?>

㈢ 如何在php中链接mysql数据库

用mysqli扩展库

<?php
//数据库主机
$db_host='localhost';
//数据库用户名
$db_user='root';
//数据库密码
$db_pwd='root';
//数据库名称
$db_name='test';
$mysqli=newMysqli($db_host,$db_user,$db_pwd,$db_name);
if(mysqli_connect_error()){
echo"connecterror";
}else{
echo"connectsuccess";
}
//关闭连接
$mysqli->close();

㈣ 如何在php中扩展mysqli插件。

Mysqli是php5之后才有的功能,没有开启扩展的可以打开php.ini的配置文件
查找下面的语句:;extension=php_mysqli.dll将其修改为:extension=php_mysqli.dll即可。
相对于mysql有很多新的特性和优势
(1)支持本地绑定、准备(prepare)等语法
(2)执行sql语句的错误代码
(3)同时执行多个sql
(4)另外提供了面向对象的调用接口的方法。

㈤ php中mysqli处理查询结果集的几个方法

$sql="select * from user"; $result=$link->query($sql); $row=$result->fetch_all(MYSQLI_BOTH);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
$n=0; while($n<mysqli_num_rows($result)){ echo "ID:".$row[$n]["id"]."用户名:".$row[$n]["name"]."密码:".$row[$n]["password"]."<br />"; $n++;
}

㈥ php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例

本文实例讲述了php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率。分享给大家供大家参考,具体如下:
<?php
/**
*
测试pdo和mysqli的执行效率
*/
header("Content-type:text/html;charset=utf-8");
//通过pdo链接数据库
$pdo_startTime
=
microtime(true);
$pdo
=
new
PDO("mysql:host=localhost;dbname=test","root","1234",array(PDO::MYSQL_ATTR_INIT_COMMAND
=>
"SET
NAMES'utf8';"));
for($i=1;$i<=100;$i++){
$title
=
"pdo标题".$i;
$content
=
"pdo内容".$i;
$addtime
=
time();
$user_id
=
$i;
$pdo_sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES(:title,:content,:addtime,:user_id)";
$sth
=
$pdo->prepare($pdo_sql);
$sth->bindParam(':title',$title);
$sth->bindParam(':content',$content);
$sth->bindParam(':addtime',$addtime);
$sth->bindParam(':user_id',$user_id);
$sth->execute();
}
$pdo_endTime
=
microtime(true);
$pdo_time
=
$pdo_endTime
-
$pdo_startTime;
echo
$pdo_time;
echo
"<hr/>";
//通过mysql链接数据库
$mysqli_startTime
=
microtime(true);
$mysqli
=
mysqli_connect("localhost","root","1234","test")
or
die("数据连接失败");
mysqli_query($mysqli,"set
names
utf8");
for($i=1;$i<=100;$i++){
$title
=
"mysqli标题".$i;
$content
=
"mysqli内容".$i;
$addtime
=
time();
$user_id
=
$i;
$sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES('".$title."','".$content."',".$addtime.",".$user_id.")";
mysqli_query($mysqli,$sql);
}
$mysqli_endTime
=
microtime(true);
$mysqli_time
=
$mysqli_endTime
-
$mysqli_startTime;
echo
$mysqli_time;
echo
"<hr/>";
if($pdo_time
>
$mysqli_time){
echo
"pdo的执行时间是mysqli的".round($pdo_time/$mysqli_time)."倍";
}else{
echo
"mysqli的执行时间是pdo的".round($mysqli_time/$pdo_time)."倍";
}
测试结果:其实经过多次测试,pdo和mysqli的执行效率差不多。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例php中数据库连接方式pdo和mysqli对比分析php中关于mysqli和mysql区别的一些知识点分析php操作mysqli(示例代码)php封装的mysqli类完整实例PHP以mysqli方式连接类完整代码实例php简单解析mysqli查询结果的方法(2种方法)php中mysql连接方式PDO使用详解Php中用PDO查询Mysql来避免SQL注入风险的方法php
mysql
PDO
查询操作的实例详解PHP实现PDO的mysql数据库操作类

㈦ 如何在php安装完成之后加入mysqli支持

修改php安装目录里面的 php.ini 文件,用记事本打开,Ctrl+f键查找下面这一行
找到后把下面这一行前面的分号去掉。重启服务器即可。
;extension=php_mysqli.dll

㈧ 腾讯云服务器下,怎么安装php的mysqli扩展

添加扩展的基本步骤:

1、进入php源代码目录:# cd /tmp/php-5.x.x/
2、再进入要添加的mysqli扩展源码目录:# cd ext/mysqli/
2、调用已经编译好的php里面的phpize:# /usr/local/php/bin/phpize
3、然后configure:# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysqli=/usr/local/mysql/bin/mysql_config
(/usr/local/mysql 为mysql的安装目录)
4、make && make install
5、编译之后,自动把mysqli.so放到了默认的php扩展目录下,我的为 /usr/local/php/lib/php/extensions/no-debug-non-zts-20xx0722/

(phpinfo可查看或者执行命令/usr/local/php/bin/php-config --extension-dir )

再修改php.ini 找到extension_dir 默认路径为 extension_dir="./" 我修改后才启动加载的

在下面添加extension = "mysqli.so" 保存即可

extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20xx0722/"

extension = "mysqli.so"

6、重启apache:# service httpd restart
求课吧有挺多IT编程类的教程的

㈨ linux下的php到底是怎么加载mysqli模块的

添加扩展的基本步骤:
1、进入PHP源代码目录:# cd /tmp/php-5.2.14/
2、再进入要添加的mysqli扩展源码目录:# cd ext/mysqli/
2、调用已经编译好的php里面的phpize:# /usr/local/php/bin/phpize
3、然后configure:# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysqli=/usr/local/MySQL/bin/mysql_config
(/usr/local/mysql 为mysql的安装目录)
4、make && make install
5、编译之后,自动把mysqli.so放到了默认的php扩展目录下,我的为 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
(phpinfo可查看或者执行命令/usr/local/php/bin/php-config --extension-dir )
再修改php.ini 找到extension_dir 默认路径为 extension_dir="./" 我修改后才启动加载的
在下面添加extension = "mysqli.so" 保存即可

extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "mysqli.so"

6、重启apache:# service httpd restart

㈩ PHP使用mysqli扩展连接MySQL数据库

1.面向对象的使用方式
$db
=
new
mysqli('localhost',
'root',
'123456',
'dbname');
如果建立连接时未指定数据库则选择使用的数据库,切换使用的数据库
$db->select_db('dbname');
$query
=
"SELECT
*
FROM
user
WHERE
uid=4";
$result
=
$db->query($query);
$result_num
=
$result->num_rows;
$row
=
$result->fetch_assoc();
//返回一个关联数组,可以通过$row['uid']的方式取得值
$row
=
$result->fetch_row();
//返回一个列举数组,可以通过$row[0]的方式取得值
$row
=
$result->fetch_array();
//返回一个混合数组,可以通过$row['uid']和$row[0]两种方式取得值
$row
=
$result->fetch_object();
//返回一个对象,可以通过$row->uid的方式取得值
$result->free();
//释放结果集
$db->close();
//关闭一个数据库连接,这不是必要的,因为脚本执行完毕时会自动关闭连接
当进行INSERT、UPDATE、DELETE操作时,使用$db->affected_rows查看影响行数
2.面向过程的使用方式
$db
=
mysqli_connect('localhost',
'root',
'123456',
'dbname');
如果建立连接时未指定数据库则选择使用的数据库,切换使用的数据库
mysqli_select_db($db,
'dbname');
查询数据库
$query
=
"SELECT
*
FROM
user
WHERE
uid=4";
$result
=
mysqli_query($db,
$query);
$result_num
=
mysqli_num_rows($result);
返回一行结果
$row
=
mysqli_fetch_assoc($result);
//返回一个关联数组,可以通过$row['uid']的方式取得值
$row
=
mysqli_fetch_row($result);
//返回一个列举数组,可以通过$row[0]的方式取得值
$row
=
mysqli_fetch_array($result);
//返回一个混合数组,可以通过$row['uid']和$row[0]两种方式取得值
$row
=
mysqli_fetch_object($result);
//返回一个对象,可以通过$row->uid的方式取得值
断开数据库连接
mysqli_free_result($result);
//释放结果集
mysqli_close($db);
//关闭一个数据库连接,这不是必要的,因为脚本执行完毕时会自动关闭连接
当进行INSERT、UPDATE、DELETE操作时,使用mysqli_affected_rows()查看影响行数

阅读全文

与mysqliphp教程相关的资料

热点内容
数控铣床法兰克子程序编程 浏览:173
linux打包命令targz 浏览:996
抖音app是哪个 浏览:407
苹果app怎么上架 浏览:255
NA服务器地址 浏览:427
我的世界如何初始化服务器 浏览:97
哪个手机app天气预报最准 浏览:752
怎样把视频压缩至25m 浏览:570
vivox27文件夹怎么改变 浏览:727
新手玩狼人杀用什么app 浏览:615
pdf在线查看 浏览:954
安卓tv90如何关闭后台 浏览:683
php读取word乱码 浏览:755
minicom源码 浏览:1001
海尔冷柜压缩机 浏览:416
联通服务器如何调试信号 浏览:136
stata新命令 浏览:941
单调栈算法python 浏览:606
微信解压游戏怎么下载 浏览:962
忍三服务器不同如何登上账号 浏览:822