用户评论:

[#1]

logos-php at kith dot orgpp [2012-03-23 17:34:02]

Turns out you can't directly use a prepared statement for a query that has a placeholder in an IN() clause.

There are ways around that (such as constructing a string that consists of n question marks separated by commas, then using that set of placeholders in the IN() clause), but you can't just say IN (?).

This is a MySQL restriction rather than a PHP restriction, but it's not really documented in the MySQL docs either, so I figured it was worth mentioning here.

(Btw, turns out someone else had previously posted the info that I put in my previous comment, about not using quotation marks. Sorry for the repeat; not sure how I missed the earlier comment.)

[#2]

logos-php at kith dot org [2012-03-23 07:06:15]

Note that if you're using a question mark as a placeholder for a string value, you don't surround it with quotation marks in the MySQL query.

For example, do this:

mysqli_stmt_prepare($stmt, "SELECT * FROM foo WHERE foo.Date > ?");

Do not do this:

mysqli_stmt_prepare($stmt, "SELECT * FROM foo WHERE foo.Date > '?'");

If you put quotation marks around a question mark in the query, then PHP doesn't recognize the question mark as a placeholder, and then when you try to use mysqli_stmt_bind_param(), it gives an error to the effect that you have the wrong number of parameters.

The lack of quotation marks around a string placeholder is implicit in the official example on this page, but it's not explicitly stated in the docs, and I had trouble figuring it out, so figured it was worth posting.

[#3]

ndungi at gmail dot com [2009-04-22 02:25:41]

The `prepare` , `bind_param`, `bind_result`, `fetch` result, `close` stmt cycle can be tedious at times. Here is an object that does all the mysqli mumbo jumbo for you when all you want is a select leaving you to the bare essential `preparedSelect` on a prepared stmt. The method returns the result set as a 2D associative array with the `select`ed columns as keys. I havent done sufficient error-checking and it also may have some bugs. Help debug and improve on it.

I used the bible.sql db from http://www.biblesql.net/sites/biblesql.net/files/bible.mysql.gz.

Baraka tele!

============================

public$connection;#establish db connectionpublic function__construct($host="localhost",$user="user",$pass="",$db="bible")

{$this->connection= newmysqli($host,$user,$pass,$db);

if(mysqli_connect_errno())

{

echo("Database connect Error : ".mysqli_connect_error($mysqli));

}

}#store mysqli objectpublic functionconnect()

{

return$this->connection;

}#run a prepared querypublic functionrunPreparedQuery($query,$params_r)

{$stmt=$this->connection->prepare($query);$this->bindParameters($stmt,$params_r);

if ($stmt->execute()) {

return$stmt;

} else {

echo("Error in$statement: ".mysqli_error($this->connection));

return0;

}

}# To run a select statement with bound parameters and bound results.

# Returns an associative array two dimensional array which u can easily

# manipulate with array functions.public functionpreparedSelect($query,$bind_params_r)

{$select=$this->runPreparedQuery($query,$bind_params_r);$fields_r=$this->fetchFields($select);

foreach ($fields_ras$field) {$bind_result_r[] = &${$field};

}$this->bindResult($select,$bind_result_r);$result_r= array();$i=0;

while ($select->fetch()) {

foreach ($fields_ras$field) {$result_r[$i][$field] = $$field;

}$i++;

}$select->close();

return$result_r;

}#takes in array of bind parameters and binds them to result of

#executed prepared stmtprivate functionbindParameters(&$obj, &$bind_params_r)

{call_user_func_array(array($obj,"bind_param"),$bind_params_r);

}

private functionbindResult(&$obj, &$bind_result_r)

{call_user_func_array(array($obj,"bind_result"),$bind_result_r);

}#returns a list of the selected field namesprivate functionfetchFields($selectStmt)

{$metadata=$selectStmt->result_metadata();$fields_r= array();

while ($field=$metadata->fetch_field()) {$fields_r[] =$field->name;

}

return$fields_r;

}

}#end of class

#An example of the DB class in use$DB= newDB("localhost","root","","bible");$var=5;$query="SELECT abbr, name from books where id > ?";$bound_params_r= array("i",$var);$result_r=$DB->preparedSelect($query,$bound_params_r);#loop thru result array and display resultforeach ($result_ras$result) {

echo$result['abbr'] ." : ".$result['name'] ."
";

}?>

[#4]

kontakt at arthur minus schiwon dot de [2008-06-16 07:22:12]

If you wrap the placeholders with quotation marks you will experience warnings like "Number of variables doesn't match number of parameters in prepared statement" (at least with INSERT Statements).

[#5]

mhradek AT gmail.com [2008-05-15 16:06:50]

A particularly helpful adaptation of this function and the call_user_func_array function:

// $params is sent as array($val=>'i', $val=>'d', etc...)

function db_stmt_bind_params($stmt, $params)

{

$funcArg[] = $stmt;

foreach($params as $val=>$type)

{

$funcArg['type'] .= $type;

$funcArg[] = $val;

}

return call_user_func_array('mysqli_stmt_bind_param', $funcArgs);

}

Thanks to 'sned' for the code.

[#6]

lukaszNOSPAMPLEASE at epas dot pl [2008-01-15 07:15:04]

i've got some bad news for you guys if you haven't found out already.

the trick with mysqli_next_result() only prevents having the connection dropped after a stored procedure call.

apparently you can bind parameters for a prepared stored procedure call, but you'll get messed up records from mysqli_stmt_fetch() after mysqli_stmt_bind_result(), at least when the stored procedure itself contains a prepared statement.

a way to avoid data corruption could be specifying the CLIENT_MULTI_STATEMENTS flag in mysqli_real_connect(), if it wasn't disabled entirely (for security reasons, as they say). another option is to use mysqli_multi_query(), but then you can't bind at all.

[#7]

st dot john dot johnson at gmail dot com [2007-06-04 08:59:28]

In reference to what lachlan76 said before, stored procedures CAN be executed through prepared statements as long as you tell the DB to move to the next result before executing again.

Example (Five calls to a stored procedure):

<?phpfor  ($i=0;$i<5;$i++) {$statement=$mysqli->stmt_init();$statement->prepare("CALL some_procedure( ? )");// Bind, execute, and bind.$statement->bind_param("i",1);$statement->execute();$statement->bind_result($results);

while($statement->fetch()) {// Do what you want with your results.}$statement->close();// Now move the mysqli connection to a new result.while($mysqli->next_result()) { }

}?>

If you include the last statement, this code should execute without the nasty "Commands out of sync" error.

[#8]

lachlan76 at gmail dot com [2006-11-28 20:59:17]

Do not try to use a stored procedure through a prepared statement.

Example:

$statement=$mysqli->stmt_init();$statement->prepare("CALL some_procedure()");?>

If you attempt to do this, it will fail by dropping the connection during the next query.  Use mysqli_multi_query instead.

Example:

$mysqli->multi_query("CALL some_procedure()");

do

{$result=$mysqli->store_result();// Do your processing work here$result->free();

} while($mysqli->next_result());?>

This means that you cannot bind parameters or results, however.

[#9]

andrey at php dot net [2005-10-07 05:35:41]

If you select LOBs use the following order of execution or you risk mysqli allocating more memory that actually used

1)prepare()

2)execute()

3)store_result()

4)bind_result()

If you skip 3) or exchange 3) and 4) then mysqli will allocate memory for the maximal length of the column which is 255 for tinyblob, 64k for blob(still ok), 16MByte for MEDIUMBLOB - quite a lot and 4G for LONGBLOB (good if you have so much memory). Queries which use this order a bit slower when there is a LOB but this is the price of not having memory exhaustion in seconds.

mysql hypot_mysqli_stmt_prepare相关推荐

  1. mysql 快捷查询数据库各表占用空间,mysql查看索引占用空间,mysql查看数据占用空间

    先贴一张图! 第一步,"很重要" 在mysql中,有一个创建之初自带的库information_schema,这个库中包含着数据库相关信息,查询数据占用空间就是使用该库,所以首先进 ...

  2. mysql并发更新数据,多用户并发修改数据解决方案。

    mysql并发更新数据,多用户并发修改数据解决方案. 在系统中,有一些如余额.资产.积分的数据,是要保证数据一致性的.如,一个人使用两个设备同时进行消费操作,如何保证数据一致性的问题. 我们一起来思考 ...

  3. mysql查询字段大小写结果相同,mysql大小写查询不敏感,mysql5.7查询不区分大小写解决方案。

    下面有两条sql,主键查询,在mysql中查询到的结果相同. SELECT* FROM USER WHEREid = 'EM58hdK4nXC';SELECT* FROM USER WHEREid = ...

  4. 数据库中自定义排序规则,Mysql中自定义字段排序规则,Oracle中自定义字段排序规则,decode函数的用法,field函数的用法

    数据库中自定义排序 场景:有一张banner表,表中有一个status字段,有0, 1, 2三个状态位,我想要 1,0,2的自定义排序(这里是重点),然后再进行之上对sequence字段进行二次排序( ...

  5. mybatis查询报错:com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from string

    mybatis查询报错: com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from strin ...

  6. docker一步安装mysql,docker的魅力就在于此

    正常来说,使用docker安装东西的步骤是serach它有没有,然后pull它 这里其实只要一步(如果你没有安装过.没有端口占用等问题的话!!) $ docker run -d -p 3306:330 ...

  7. 设置腾讯云linux服务器中 MySQL 允许远程访问

    申请了一台linux腾讯云服务器,想要把数据库搭建在上面,本地的Windows直接可以访问 以下就是具体的操作流程,首先你需要安装好一个mysql,安装方法–>mysql安装(Linux) 接着 ...

  8. Linux安装mysql,一步到位!

    今天在腾讯云上面买了一个服务器,想要把自己的项目部署一下,就要安装mysql,以下是我的安装步骤,在网上有很多人把install敲错了,还有的少-get,种种错误试完之后,我决定发一篇 sudo ap ...

  9. Mysql函数group_concat、find_in_set 多值分隔字符字段进行数据库字段值翻译

    Mysql函数group_concat.find_in_set进行数据库字段值翻译 场景 配方表:记录包含的原料 sources表示原料,字段值之间用逗号分隔 原料表:对应原料id和原料名称 现需要查 ...

最新文章

  1. 6.QT信号槽的时序分析
  2. 功能奇数次执行和偶数次执行时的结果不同的故障复盘
  3. 代谢组学的相关分析数据库,MetaboAnalyst 5.0 使用指南
  4. UI开发的终极解决方案
  5. 云起实验室有奖征文,精美好礼等你来领——《我的linux初体验》
  6. Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
  7. Java状态和策略设计模式之间的差异
  8. html双击变成可编辑,jquery 实现双击编辑并保存
  9. python 分类变量回归_极简Python带你探索分类与回归的奥秘
  10. 计算机视觉论文-2021-06-21
  11. NoteExpress的题录中文??问题
  12. 数据库挖掘 概念 定义 什么是数据挖掘
  13. 我喜欢夜晚,黑黑的夜色带给人安全感
  14. c 语言程序编辑,C语言程序编辑学习篇—编程者说之C语言
  15. div大小拖动调节及鼠标样式修改
  16. 使用python进行数据预处理--主成分分析
  17. 知到大学生心理健康第一章单元测试
  18. 万字带图教程带你从零开始安装CentOS
  19. 2018年电赛A题(电流检测设备)的复刻
  20. 程序员们,阿里、腾讯和百度的公司职级、薪资待遇,你有了解吗?

热门文章

  1. 武侠q传服务器维护,《武侠Q传》就服务器人多过载致歉玩家赞有诚意
  2. 计算机专业哪些证书可以抵个税,2020年度个人所得税汇算清缴进行时 职业资格证书有哪些能抵扣个税?...
  3. 原码、反码、补码、移码的表示
  4. 回溯法遵循深度优先吗_闲来刷下「回溯算法」
  5. iframe 按比例缩放_不按常理出牌!5个Excel灵异事件,能让你崩溃
  6. python six库_six库 解决python2的项目如何能够完全迁移到python3
  7. fastdfs 测试客户端_分布式文件服务 FastDFS (第一篇)
  8. python中split_python中split()和split(' ')的区别
  9. 谷歌浏览器chrome的vuejs devtools 插件的安装
  10. java 可用内存_总可用内存java