本文目录一览:
- 1、请问php+mysql如何实现写入数据成功全成功,失败全失败?
- 2、php mysqli 常用函数有哪些
- 3、thinkphp mysql语句 sum
- 4、dw php mysql记录集分页
- 5、php mysql 事务怎么写
请问php+mysql如何实现写入数据成功全成功,失败全失败?
可以使用MYSQL事务处理,默认的MYSQL是不支持事务的,需要在mysql.ini中开启。 成功开启后,假设你的连接为 $conn; 表结构 tab id 自动增长,主键 val varchar(20) mysql_query('BEGIN',$conn); $data = array(1,'NULL',3,4,5,6,7,8,9,10,11);//不全正确的数据 for($i=0;$i10;$i++) { $query = mysql_query("INSERT INTO tab(id,val) VALUE ('".$data[$i]."','value_$i')",$conn);//只有第二条插入成功 if(!$query) { //执行失败,马上结束循环并回滚。 mysql_query('ROLLBACK',$conn); break; }else{ //执行成功 mysql_query('COMMIT',$conn); } } mysql_query("END",$conn);
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-phpmysqlbegin
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-phpmysqlbegin
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
以上函数清单直接来自 网站。你可以进入该网站参看。
thinkphp mysql语句 sum
你的问题有N多问题,说说我的建议,
通常,假如一张表纪录包子米饭的销售纪录
ID 售出类型 售出数量 售出时间
id type num time
1 baozi 10 2013.08.01 15:30
2 mifan 21 2013.08.01 11:30
查米饭今天的销售纪录 就
select sum(num) from table where type = 'mifan' and time 2013.08.01 00:00:00 and time 2013.08.01 23:59:59-phpmysqlbegin
当然这里的时间比较只是比较直观点的,具体比较时肯定用时间戳格式的,,,,
简单明了,
然后回到你的问题,
你已经查询出了1.1号的包子销量,你现在需要做的就是要查出1.1号的米饭数量对吧
TP我以前玩过,是MVC的,这样的话,你其实可以这样,
在取当天销售包子的数据时,根据日期查询当天的米饭数量,然后一同放进数组,然后的模板里直接打印,
直观点就是原本一个数组
?php
$data=array('20130101'=array('baozi'=12),
'20130102'=array('baozi'=16));
foreach($data as $k=$v){
select sum(mifan) from table where 时间在20130101这天,
得到数量$n
$data[$k]['mifan']=$n;
}
这样下来数组$data就会变成:
$data=array('20130101'=array('baozi'=12,'mifan'=...),
'20130102'=array('baozi'=16,'mifan'=...));
你直接在模板文件里取对应字段值就可以了,
因为没看到你的代码,所以解释的可能不清,有问题欢迎追问,
追问
Select url, Count(*) As c From `".$begin."` where is_uid='1' and ref_type='sina' group by url ORDER BY `c` desc-phpmysqlbegin
$begin 是一个表,表是按照时间命名的
dw php mysql记录集分页
用一个分页类。直接调用。
?php
/**
* 分页类
* 用DBMySQL类读取数据库
*/
class BreakPage
{
//属性开始
var $pageparam="pageno";
var $sql;//显示数据的sql
var $countsql;//显示总记录数的SQL
var $max_line;//最大记录数
var $begin_record;//开始记录数
var $current_page;//当前页
var $total_record;//总记录数
var $total_page;//总页数
var $conn_id;//数据库连接
var $query_id;//查询句柄
var $row_num;//当页记录的条数
var $url;//url
var $param="";
//属性结束
function setpageparam($pageparam)
{
$this-pageparam=$pageparam;
}
function getpageparam()
{
return $this-pageparam;
}
function setrows($rows)
{
$this-row_num=$rows;
}
function getrows()
{
return $this-row_num;
}
function setparam($param)
{
$this-param=$param;
}
function getparam()
{
return $this-param;
}
function seturl($url)
{
$this-url=$url;
}
function geturl()
{
return $this-url;
}
//属设置开始
function setcountsql($countsql)
{
$this-countsql="select count(*) as count from (".$countsql.") as temp1";
}
function getcountsql()
{
return $this-countsql;
}
function settotalpage($totalpage)
{
$this-total_page=$totalpage;
}
function gettotalpage()
{
return $this-total_page;
}
function settotalrecord($totalrecord)
{
$this-total_record=$totalrecord;
}
function gettotalrecord()
{
return $this-total_record;
}
/**
* 设置当前页,从1开始
*
* @param unknown_type $currentpage
*/
function setcurrentpage($currentpage)
{
$this-current_page=$currentpage1? 1:$currentpage;
}
function getcurrentpage()
{
return $this-current_page;
}
function setbeginline($begin)
{
$this-begin_record=$begin;
}
function getbeginline()
{
return $this-begin_record;
}
function setsql($sql)
{
$this-sql=$sql;
}
function getsql()
{
return $this-sql;
}
/**
* 设置最大记录数
* @param unknown_type $maxline
*/
function setmaxline($maxline)
{
$this-max_line=$maxline;
}
function getmaxline()
{
return $this-max_line;
}
//属性设置结束
function __construct()
{//连接数据库
$this-setbeginline(0);
$this-setmaxline(10);
}
function __destruct()
{
}
//方法开始
/**
* 主体函数,查询当前页的记录,同时返回信息
* @param string $sql:查询的SQL,如select * from table1;
* @param string $countsql:总数量的SQL,如select count(*) from table1;
* @return 当前页的记录
*/
function getrecords($db,$sql)
{
$this-setsql($sql);//设置查询的SQL
$this-setcountsql($sql);//设置查询总数量的SQL
//查询总记录数,可用gettotalrecord()方法返回总记录数
$db-query($this-getcountsql());
$db-seek(0);
//返总记录数
$this-settotalrecord($db-f(0));
$db-free();
//根据当前页和每页最大记录数计算开始和结束记录
//可用getbeginline()方法查询开始记录值,即limit的开始值
//可用getmaxline()方法查询每页记录数,即limit的第二个限制值
$this-setbeginline(($this-getcurrentpage()-1)*$this-getmaxline());//设置开始记录值,即limit的开始值
//计算结束
//计算总页数
//可用gettotalpage()查询总页数
$this-settotalpage((int)(($this-gettotalrecord()+$this-getmaxline()-1)/$this-getmaxline()));//(总记录数+每页记录数-1)/每页记录数-phpmysqlbegin
//计算结束
//返回当前记录
$temp_sql=$this-getsql()." limit ".$this-getbeginline().",".$this-getmaxline();//构造限制的SQL语句
$db-query($temp_sql);
$result=$db-getalldata();
return $result;
//结束
}
function paint()
{
$prepg=$this-getcurrentpage()=1? 1:$this-getcurrentpage()-1;//上一页
$nextpg=$this-getcurrentpage()=$this-gettotalpage()? $this-gettotalpage():$this-getcurrentpage()+1;//下一页-phpmysqlbegin
$otherparam=$this-getparam()!=""? "".$this-getparam():"";
$pagenav="tabletr";
if($this-getcurrentpage()==1)
{//第一页
$pagenav.="td nowrap='nowrap' style='font-size: 12px'a disabled='disabled'首页/a/tdtd nowrap='nowrap' style='font-size: 12px' /td";-phpmysqlbegin
}
else
{
$pagenav.="td nowrap='nowrap' style='font-size: 12px'a href='".$this-geturl()."?".$this-getpageparam()."=1$otherparam'首页/a/tdtd nowrap='nowrap' style='font-size: 12px' /td";-phpmysqlbegin
}
if($this-getcurrentpage()1)
{//有上一页
$pagenav.="td nowrap='nowrap' style='font-size: 12px'a href='".$this-geturl()."?".$this-getpageparam()."=$prepg$otherparam'上一页/a/td";-phpmysqlbegin
}
else
{//第一页
$pagenav.="td nowrap='nowrap' style='font-size: 12px'a disabled='disabled'上一页/a/td";
}
if($this-getcurrentpage()$this-gettotalpage())
{//有下一页
$pagenav.="td nowrap='nowrap' style='font-size: 12px' a href='".$this-geturl()."?".$this-getpageparam()."=$nextpg$otherparam'下一页/a/td";-phpmysqlbegin
}
else
{
$pagenav.="td nowrap='nowrap' style='font-size: 12px' a disabled='disabled'下一页/a/td";
}
if($this-getcurrentpage()=$this-gettotalpage())
{//没有下一页
$pagenav.="td nowrap='nowrap' style='font-size: 12px'/tdtd nowrap='nowrap' style='font-size: 12px'a disabled='disabled'尾页/a/td";-phpmysqlbegin
}
else
{
$pagenav.="td nowrap='nowrap' style='font-size: 12px'/tdtd nowrap='nowrap' style='font-size: 12px'a href='".$this-geturl()."?".$this-getpageparam()."=".$this-gettotalpage()."$otherparam'尾页/a/td";-phpmysqlbegin
}
//下拉跳转列表,循环列出所有页码:
$onchange=$otherparam!=""? 'window.location="'.$this-url.'?'.$this-getpageparam().'="+this.value+"'.$otherparam.'"':'window.location="'.$this-url.'?'.$this-getpageparam().'="+this.value';-phpmysqlbegin
$pagenav.="td nowrap='nowrap' style='font-size: 12px' 共font color=red".$this-gettotalpage()."/font页/tdtd nowrap='nowrap' style='font-size: 12px' 第font color=red".$this-getcurrentpage()."/font页/tdtd nowrap='nowrap' style='font-size: 12px' 转到第/tdtd nowrap='nowrap'select name='topage' size='1' onchange='$onchange'";-phpmysqlbegin
for($i=1;$i=$this-gettotalpage();$i++)
{
if($i==$this-getcurrentpage())
{
$pagenav.="option value='$i' selected$i/option\n";
}
else
{
$pagenav.="option value='$i'$i/option\n";
}
}
$pagenav.="/select/tdtd nowrap='nowrap' style='font-size: 12px'页/td";
$pagenav.="/tr/table";
return $pagenav;
}
//方法结束
}
?
php mysql 事务怎么写
mysql_query("begin");
mysql_query("commit");
mysql_query("rollback");
mysql_query("end");
直接这样写就可以了