{"id":213,"date":"2009-07-30T09:04:25","date_gmt":"2009-07-30T07:04:25","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=213"},"modified":"2009-08-21T09:04:05","modified_gmt":"2009-08-21T07:04:05","slug":"php-mysqli-and-dynamic-parameter-binding","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=213","title":{"rendered":"PHP, MySQLi and Dynamic Parameter Binding"},"content":{"rendered":"<p>I worked with PHP and MySQLi for the last few days and still don&#8217;t like it that much but got used to it.<\/p>\n<p>Something that was annoying me pretty much was the Binding of Parameters in an SQL-Statement.<br \/>\nSo I decided to write a little Helper Function for this. <!--more--><\/p>\n<p>A normal Binding looks like this:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\n$stmt = $db-&gt;prepare('UPDATE SET Name=?, SomeInt=? FROM SomeTable WHERE ID = ?');\r\n$stmt-&gt;bind_param('sii', $newName, $newInt, $id);\r\n<\/pre>\n<p>It&#8217;s a shame that it&#8217;s not possible to pass an Array of Objects to the <code>bind_param<\/code><\/p>\n<p>After some search I found a Function for that problem:<\/p>\n<pre class=\"brush: php; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/ $stmt = The SQL Statement Object\r\n\/\/ $param = Array of the Parameters\r\nfunction DynamicBindVariables($stmt, $params)\r\n{\r\n\tif ($params != null)\r\n\t{\r\n\t\t\/\/ Generate the Type String (eg: 'issisd')\r\n\t\t$types = '';\r\n\t\tforeach($params as $param)\r\n\t\t{\r\n\t\t\tif(is_int($param)) {\r\n\t\t\t\t\/\/ Integer\r\n\t\t\t\t$types .= 'i';\r\n\t\t\t} elseif (is_float($param)) {\r\n\t\t\t\t\/\/ Double\r\n\t\t\t\t$types .= 'd';\r\n\t\t\t} elseif (is_string($param)) {\r\n\t\t\t\t\/\/ String\r\n\t\t\t\t$types .= 's';\r\n\t\t\t} else {\r\n\t\t\t\t\/\/ Blob and Unknown\r\n\t\t\t\t$types .= 'b';\r\n\t\t\t}\r\n\t\t}\r\n \r\n\t\t\/\/ Add the Type String as the first Parameter\r\n\t\t$bind_names&#x5B;] = $types;\r\n \r\n\t\t\/\/ Loop thru the given Parameters\r\n\t\tfor ($i=0; $i&lt;count($params);$i++)\r\n\t\t{\r\n\t\t\t\/\/ Create a variable Name\r\n\t\t\t$bind_name = 'bind' . $i;\r\n\t\t\t\/\/ Add the Parameter to the variable Variable\r\n\t\t\t$$bind_name = $params&#x5B;$i];\r\n\t\t\t\/\/ Associate the Variable as an Element in the Array\r\n\t\t\t$bind_names&#x5B;] = &amp;$$bind_name;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ Call the Function bind_param with dynamic Parameters\r\n\t\tcall_user_func_array(array($stmt,'bind_param'), $bind_names);\r\n\t}\r\n\treturn $stmt;\r\n}\r\n<\/pre>\n<p>Now you can Bind the Parameters like this:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\n$paramList = array();\r\n$paramList&#x5B;] = $newName;\r\n$paramList&#x5B;] = $newInt;\r\n$paramList&#x5B;] = $id;\r\n$stmt = $db-&gt;prepare('UPDATE SET Name=?, SomeInt=? FROM SomeTable WHERE ID = ?');\r\nDynamicBindVariables($stmt, $paramList);\r\n<\/pre>\n<p>The Code is a bit longer but we&#8217;re now able to easily generate Statements with a dynamic amount of Parameters and bind them correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I worked with PHP and MySQLi for the last few days and still don&#8217;t like it that much but got used to it. Something that was annoying me pretty much was the Binding of Parameters in an SQL-Statement. So I decided to write a little Helper Function for this.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6,3],"tags":[91,92,89,90],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"has-more-link","5":"post-213","7":"format-standard","8":"category-php","9":"category-programming","10":"post_tag-binding","11":"post_tag-dynamic","12":"post_tag-mysqli","13":"post_tag-parameter"},"acf":[],"views":296000,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/213"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}