/    Sign up×
Community /Pin to ProfileBookmark

How to bind parameters dynamically

I want to bind mysqli parameters dynamically using php. The problem I have is adapting my code to do this. I have seen a question which should be helpful to be able to solve this problem which is [URL=”http://stackoverflow.com/questions/5100046/how-to-bind-mysqli-bind-param-arguments-dynamically-in-php”]http://stackoverflow.com/questions/5100046/how-to-bind-mysqli-bind-param-arguments-dynamically-in-php[/URL] but I dont know how to use that code to adapt it into my code. I am really struggling to understand it. It will be easier for me to see somebody adapt my code to fit the bill so then I can understand what is going on.

Can somebody help guide me what I need to do to be able to bind the parameters dynamically in my code? Do I just need to change the bind param method to a call_func_user_array or is it much more than that?

Below is my code:

[CODE]
$questionquery = “SELECT q.QuestionContent FROM Question q WHERE “;

$i=0;

$whereArray = array();
$orderByArray = array();
$orderBySQL = “”;
$paramString = “”;

//loop through each term
foreach ($terms as &$each) {
$i++;
//if only 1 term entered then perform this LIKE statement
if ($i == 1){
$questionquery .= “q.QuestionContent LIKE ? “;
} else {
//If more than 1 term then add an OR statement
$questionquery .= “OR q.QuestionContent LIKE ? “;
$orderBySQL .= “,”;
}

$orderBySQL .= “IF(q.QuestionContent LIKE ? ,1,0)”;

$whereArray[] = “%” . $each . “%”;
$orderByArray[] = “%” . $each . “%”;

$paramString .= “ss”;
}

$questionquery .= “GROUP BY q.QuestionId, q.SessionId ORDER BY ” . $orderBySQL;
$stmt=$mysqli->prepare($questionquery)or die($mysqli->error); ;
$stmt->bind_param($paramString, array_merge($whereArray, $orderByArray));
$stmt->execute();
$stmt->bind_result($dbQuestionContent);
$questionnum = $stmt->num_rows();
[/CODE]

Thank you and I appreciate your help ?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @carlbrooks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...