首页 > 编程知识 正文

php执行mysql语句的函数(mysql与php)

时间:2023-12-24 12:06:03 阅读:321463 作者:EROB

本文目录一览:

php mysqli 常用函数有哪些

php  中 mysqli 是个类,这个类的函数(方法)有:

mysqli::$affected_rows — Gets the number of affected rows in a previous MySQL operation

mysqli::autocommit — 打开或关闭本次数据库连接的自动命令提交事务模式

mysqli::begin_transaction — Starts a transaction

mysqli::change_user — Changes the user of the specified database connection

mysqli::character_set_name — 返回当前数据库连接的默认字符编码

mysqli::$client_info — Get MySQL client info

mysqli::$client_version — Returns the MySQL client version as a string

mysqli::close — 关闭先前打开的数据库连接

mysqli::commit — 提交一个事务

mysqli::$connect_errno — Returns the error code from last connect call

mysqli::$connect_error — Returns a string description of the last connect error

mysqli::__construct — Open a new connection to the MySQL server

mysqli::debug — Performs debugging operations

mysqli::dump_debug_info — 将调试信息输出到日志

mysqli::errno — 返回最近函数调用的错误代码

mysqli::$error_list — Returns a list of errors from the last command executed

mysqli::$error — Returns a string description of the last error

mysqli::$field_count — Returns the number of columns for the most recent query

mysqli::get_charset — Returns a character set object

mysqli::get_client_info — Get MySQL client info

mysqli_get_client_stats — Returns client per-process statistics

mysqli_get_client_version — 作为一个整数返回MySQL客户端的版本

mysqli::get_connection_stats — Returns statistics about the client connection

mysqli::$host_info — 返回一个表述使用的连接类型的字符串

mysqli::$protocol_version — 返回MySQL使用的协议版本号

mysqli::$server_info — 返回MySQL服务器的版本号

mysqli::$server_version — 作为一个整数返回MySQL服务器的版本

mysqli::get_warnings — Get result of SHOW WARNINGS

mysqli::$info — Retrieves information about the most recently executed query

mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()

mysqli::$insert_id — Returns the auto generated id used in the last query

mysqli::kill — Asks the server to kill a MySQL thread

mysqli::more_results — Check if there are any more query results from a multi query

mysqli::multi_query — Performs a query on the database

mysqli::next_result — Prepare next result from multi_query

mysqli::options — Set options

mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down

mysqli::poll — Poll connections

mysqli::prepare — Prepare an SQL statement for execution

mysqli::query — 对数据库执行一次查询

mysqli::real_connect — 建立一个 MySQL 服务器连接

mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection

mysqli::real_query — 执行一个mysql查询

mysqli::reap_async_query — Get result from async query

mysqli::refresh — Refreshes

mysqli::release_savepoint — Removes the named savepoint from the set of savepoints of the current transaction

mysqli::rollback — 回退当前事务

mysqli::rpl_query_type — Returns RPL query type

mysqli::savepoint — Set a named transaction savepoint

mysqli::select_db — 选择用于数据库查询的默认数据库

mysqli::send_query — 发送请求并返回结果

mysqli::set_charset — 设置默认字符编码

mysqli::set_local_infile_default — Unsets user defined handler for load local infile command

mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command

mysqli::$sqlstate — Returns the SQLSTATE error from previous MySQL operation

mysqli::ssl_set — Used for establishing secure connections using SSL

mysqli::stat — Gets the current system status

mysqli::stmt_init — 初始化一条语句并返回一个用于mysqli_stmt_prepare(调用)的对象

mysqli::store_result — Transfers a result set from the last query

mysqli::$thread_id — Returns the thread ID for the current connection

mysqli::thread_safe — 返回是否是线程安全的

mysqli::use_result — Initiate a result set retrieval

mysqli::$warning_count — Returns the number of warnings from the last query for the given link

以上函数清单直接来自  网站。你可以进入该网站参看。

关于php操作mysql执行数据库查询的一些常用操作汇总

php操作mysql步骤:

1.$connect=mysql_connect('localhost','root','123456')

or

die('数据库连接失败。'mysql_error());链接mysql。

2.mysql_select_db('database',$connect)选择链接的数据库。

3.mysql_query('Set

names

gb2312');$sql

=

"select

*

from

blog_article";准备要查询的数据。

4.$datas

=

mysql_query($sql);执行sql查询。

5.$data

=

mysql_fetch_assoc($datas)得到查询到的缓存在内存中的一条数据。

6.print_r($data);

相同点:三个函数都是返回数据库中查询到的一行数据(说的再清楚点就是一条数据)。

不同点:mysql_fetch_assoc()用的是数据库中相应的字段名作为的key值(也就是数组下标)

如:filed['id']=1;

mysql_fetch_row()用的是自动生成的数字(从0开始依次生成)作为的key值(也就是数组下标)

如:filed[0]=1;

mysql_fetch_array()用的是自动生成的数字(从0开始依次生成)作为的key值(也就是数组下标),而且它还同时生成数据库中相应的字段名作为的key值(也就是数组下标)

如:

filed[0]=1,filed['id']=1;也就是说,mysql_fetch_array()将mysql_fetch_assoc()和mysql_fetch_row()查询到的结果合为了一体了。

mysql_fetch_object()与mysql_fetch_assoc()差不多。只是mysql_fetch_assoc()返回的是数组。mysql_fetch_object()返回的是object对象。

mysql_insert_id() 取得上一步

INSERT

操作产生的

ID。

mysql_result()

函数返回结果集中一个字段的值。

mysql_num_fields()

函数返回结果集中字段的数目。

mysql_affected_rows();返回前一次

MySQL

操作所影响的记录行数。

mysql_num_rows(mysql_query($sql))获得结果集中行的数目。

mysql_pconnect()

函数打开一个到

MySQL

服务器的持久连接。

mysql_pconnect()

mysql_connect()

非常相似,但有两个主要区别:

1.

当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。

2.

其次,当脚本执行完毕后到

SQL

服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close()

不会关闭由

mysql_pconnect()

建立的连接)。

mysql_data_seek(mysql_query($sql),8);获得结果集中的第8条数据。(mysql_num_rows(mysql_query($sql))和mysql_data_seek(mysql_query($sql),8)在mysql_unbuffered_query($sql)不可以使用。)

mysql_unbuffered_query($sql)和mysql_query($sql)效果差不多,但是

mysql_unbuffered_query($sql)不缓存。mysql_query($sql)会缓存查询的结果。

mysql_close();关闭mysql的最近的链接。

mysql_field_flags(mysql_query($sql),6)返回第六个字段的表属性输出如:not_null

primary_key

auto_increment

mysql_fetch_lengths(mysql_query($sql))返回该条数据的所有字段的每个字段的长度。返回的是一个数字组成的数组。

mysql_field_name(mysql_query($sql),3)返回第三个字段的字段名。

mysql_field_table(mysql_query($sql),0)返回指定字段所在的表名。

mysql_free_result(mysql_query($sql))

函数释放结果内存。

mysql_get_client_info()

函数返回

MySQL

客户端信息。

mysql_get_host_info()

取得

MySQL

主机信息。

在PHP程序中,执行Mysql命令操作的语句是??

第一个函数是链接后台数据库服务

第二个函数是选择数据库

第三个函数是数据迭代器

第四个函数是执行数据库操作语句

PHP执行批量mysql语句的解决方法

当有多条mysql语句连起来需要执行,比如

$sqls=

“insert

table

a

values(1,2);

insert

table

a

values(2,3);”

需要执行的话php中可以使用的方法有三个:

mysql_query

pdo

mysqli

三种方法当sqls语句没有问题的时候都是可以的。

但是

当sql语句是错误的时候会出现问题

第一条sql错误:三个方法都返回false

第一条sql正确,第二条sql错误:mysql_query、pdo、

mysqli:query也是返回true。所以这个时候你是没法判断你的sqls是否有那条语句是错误的。

解决这种办法有几个招:

1

解析sql语句

将每条sql都拆分开来执行。这样每个语句分开执行就解决了。但是这种方法多出了好几种方法,所以不可取。

2

将sqls语句保存为文本

使用cmd执行命令

mysql….

.

sqls.sql,

然后捕获输出。这也是一种方法,但是感觉是绕着问题走,应该还有更好的方法。

3

使用mysqli::multi_query方法

这个方法可以执行多条sql语句,然后使用mysqli::next_result来设置sql的偏移量,使用mysqli::error获取当前偏移的sql的错误状态

下面是第三种方法的示例代码

代码如下:

复制代码

代码如下:

$sql

=

Config::get('sql');

$content

=

file_get_contents($sql);

$config

=

Config::get('config')

$mysqli

=

mysqli_connect($config['host'],

$config['user'],

$config['password'],

$config['dbname']);

$ret

=

$mysqli-multi_query($content);

if($ret

===

false)

{

echo

mysqli_error($mysqli);

}

while

(mysqli_more_results($mysqli))

{

if

(mysqli_next_result($mysqli)

===

false)

{

echo

mysqli_error($mysqli);

echo

"rn";

break;

}

}

$mysqli-close();

这样的话当sqls语句中任意一条有错误的话,程序就会跳出这个错误。

如果你要编写初始化mysql的脚本的话,这招就非常好用了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。