MySQLi bind_param arguments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zoomerz
Forum Newbie
Posts: 2
Joined: Fri Jan 19, 2007 5:05 pm

MySQLi bind_param arguments

Post by zoomerz »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All;

I'm trying to find a good way to dynamically fill the "variable" arguments to a mysqli_bind_param() method. I have incoming $_GET vars, which if !empty, need to be passed to the bind_param. In code:

Code: Select all

$myFirstVar = !empty($_GET['var']) ? $_GET['var'] : false;
$mySecondVar = !empty($_GET['var']) ? $_GET['var'] : false; (etc)

$aryFields[] = $myFirstVar ? "firstField = ?";
$aryFields[] = $mySecondVar ? "secondField = ?"; (etc)

/*I'm also using an array to hold the correspondint "?" types, which are then imploded into the first bind_param('string of types'), which results in some string like 'sissi' or whatever*/

//the statement is dynamically built if the ary members exist
$sql = "UPDATE myTable SET firstField = ?, secondField = ? WHERE some condition exists";

**this is the part that's thowing me***

/*I need to now call $db->bind_param('ississ', $arg1, $arg2) dynamically for only those fields that are to be updated. I'm looking for a good way to create $arg1, arg2 as necessary. I can create a single string easily enough from a dynamic array, but then there's a mismatch of types->arguments to replace.
What I really need (I think) is an array_func ??? that will seperate the array members into comma delimited string variable arguments (not a single string argument)*/

Any ideas? And thanks!

Zoomerz (aka Randy)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Very raw thing.Untested. With little adjustments should work.
As you have probably already guessed you cannot tell apart blob type from string type.
So when you have to use blobs you should provide $fieldsType manually.

Code: Select all

<?php
function fetchBindParams($inputValues)
    {

        $return = '';
        foreach ($inputValues as $value) {
            switch (true) {
            	case is_integer($value):
                        $return .= 'i';
            		break;
            	case is_double($value):
                        $return .= 'd';
            		break;
            	case is_string($value):
                        $return .= 's';
            		break;
            	case is_null($value):
            	case is_array($value):
            	case is_object($value):
            	case is_resource($value):
            	case is_bool($value):
            	    throw new sqlexception("Unacceptable type used for bind_param.");
            	default://unknown type,
            	   throw new sqlexception("Unknown type used for bind_param.");
        		break;
            }
        }
        return $return;
    }

function getAll($query,$prepareData = array(),$fieldsType = 'auto') {

            $stmt = new mysqli_stmt();
            $stmt->prepare($query);
            $bind_params = ($fieldsType == 'auto') ? (array) fetchBindParams(array_values($prepareData)) : (array) $fieldsType;
            $params = array_merge($bind_params,$prepareData);
            @call_user_func_array(array($stmt,"bind_param") , $params);
            $stmt->execute();
            $result = array();
            while ($data = $stmt->fetch_assoc()) {
                $result[] = $data;
            }
            $stmt->close();
            return $result;

 }

$query = "SELECT * FROM Table_Name where id = ?";
$data = getAll($query,array(1));


?>
zoomerz
Forum Newbie
Posts: 2
Joined: Fri Jan 19, 2007 5:05 pm

Post by zoomerz »

Thanks jmut; Much more elegant than what i came up with. I did play around with call_user_func_array() some but couldn't make it work! I'll do some testing with this and let you know how it goes. I suppose I was under the impression that one could only call a "user defined" function with it (which bind_param is not (or did you just not implement it?)). Again, thanks! If I can get this working properly, I'll add a method to my db class for it!

Regards,
z-
Post Reply