Link to home
Start Free TrialLog in
Avatar of cescentman
cescentmanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Creating a Dynamic MySQLi Bind

I am a newbie to MySQLi and am trying out some simple examples.

So I have this code:-

<?php
	require "conMushingDB.php";

	if (!isset($sqlDeleted)) {
  	      $sqlDeleted = '0 ';
    	}
	$arrAddressWHERE = array('Address_Id',2);


	$objAddress = $mysqli->prepare("SELECT * FROM address WHERE Deleted = ?" . $strSQLAND);
	$objAddress->bind_param("i", $arrAddressWHERE[1]);
	$objAddress->bind_param("i" . $arrAddressField[$arrAddressWHERE[0]], $sqlDeleted , $arrAddressWHERE[1]);
?>

Open in new window


This works fine. However I am trying to create the facility to be able to also run the select on the whale table without the AND, but I can't find a way of doing this as when I try and add the second comma conditionally it fails to work. I was hoping I could build a statement to bind where I could make the latter half and second variable type null such as:-

$strBind = "i" . $arrAddressField[$arrAddressWHERE[0]] . ", " .  $sqlDeleted  . $strComma . $arrAddressWHERE[1];
$objAddress->bind_param($strBind );

Open in new window


... but it doesn't work (I tried adding quotes round the variable type too using \" ) but to no avail. Any ideas anyone?
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

There's way too little information here. You're using many different variables without telling us what values they contain, so it's anyone's guess as to why something does or doesn't work.

At first glance, it looks like you're trying to create programmatic bindings using just a string like this:

$strBind = "ii, 0, 2";
$objAddress->bind_param($strBind );

...but "ii, 0, 2" is just a single string - it is not several different values, even if you add commas.

If you want to call bind_param with a dynamic number of parameters, I'm not sure off the top of my head if there's any way to do it directly, but you could probably use call_user_func() to call it with the number of parameters you want.
Not sure what you're after but the bind_param method takes 2 or more arguments. The first is a list of types and the remaining arguments are the values for those types. The types parameter indicates whether you are passing in integer / string etc, and you must follow this up with the correct number of values, so if your types argument is 4 character ("isss" for example), then the bind_param method must take 5 arguments - 1 for the types and 4 for the values

$stmt->bind_param("isss", $intValue, $strValue1, $strValue2, $strValue4);

In your code, you seem to concatenating a series of strings into one variable and then passing that into bind_param, which clearly won't work.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cescentman

ASKER

Thanks @gr8gonzo and @Chris Stanyon for responding. @gr8gonzo you're code and comments are a great insight. I take your point about the lock down. I'm not sure I would use this in a real life situation but I wanted to explore.