note 102917 added to mysqli-stmt.bind-param

From: Date: Mon, 14 Mar 2011 22:28:02 +0000
Subject: note 102917 added to mysqli-stmt.bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-177873@lists.php.net to get a copy of this message
Blob and null handling aside, a couple of notes on how param values are automatically converted and forwarded on to the Mysql engine based on your type string argument: 1) PHP will automatically convert the value behind the scenes to the underlying type corresponding to your binding type string. i.e.: <?php $var = true; bind_param('i', $var); // forwarded to Mysql as 1 ?> 2) Though PHP numbers cannot be reliably cast to (int) if larger than PHP_INT_MAX, behind the scenes, the value will be converted anyway to at most long long depending on the size. This means that keeping in mind precision limits and avoiding manually casting the variable to (int) first, you can still use the 'i' binding type for larger numbers. i.e.: <?php $var = '429496729479896'; bind_param('i', $var); // forwarded to Mysql as 429496729479900 ?> 3) You can default to 's' for most parameter arguments in most cases. The value will then be automatically cast to string on the back-end before being passed to the Mysql engine. Mysql will then perform its own conversions with values it receives from PHP on execute. This allows you to bind not only to larger numbers without concern for precision, but also to objects as long as that object has a '__toString' method. This auto-string casting behavior greatly improves things like datetime handling. For example: if you extended DateTime class to add a __toString method which outputs the datetime format expected by Mysql, you can just bind to that DateTime_Extended object using type 's'. i.e.: <?php // DateTime_Extended has __toString defined to return the Mysql formatted datetime $var = new DateTime_Extended; bind_param('s', $var); // forwarded to Mysql as '2011-03-14 17:00:01' ?> ---- Server IP: 64.71.164.2 Probable Submitter: 65.36.73.107 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-param.php Edit -- https://master.php.net/note/edit/102917 Del: integrated -- https://master.php.net/note/delete/102917/integrated Del: useless -- https://master.php.net/note/delete/102917/useless Del: bad code -- https://master.php.net/note/delete/102917/bad+code Del: spam -- https://master.php.net/note/delete/102917/spam Del: non-english -- https://master.php.net/note/delete/102917/non-english Del: in docs -- https://master.php.net/note/delete/102917/in+docs Del: other reasons-- https://master.php.net/note/delete/102917 Reject -- https://master.php.net/note/reject/102917 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177873) next »