Reputation: 449
I am using a for loop to create n prepared statements, and incrementing a number in these statements. I am having a problem with the query where these numbers are being used. Last1 is the largest number used the last time this was run + 1, and last 2 is the largest number we will use this time. Here is the loop and query:
$last1 = 102;//these are normally pulled from the db, but I am specifying them here
$last2 = 104;//they are being pulled correctly, I already checked. I even tried it with specifying the variables, like this, same result.
for($i = 1; $i <= $n; $i++){
$q[$i] = "INSERT INTO outboundApps (fk_outboundAppKey, name, browser, fk_urls, fk_routes, virtualPlatform, autoAnswer, fk_GET, fk_callRates)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt[$i] = mysqli_prepare($con, $q[$i]) or trigger_error(mysqli_error($con), E_USER_ERROR);
$stmt[$i]->bind_param("issiisiis", $last1, $arr[$i][$x]/*name*/, $arr[$i][$x=$x+1]/*type/browser*/, $last1, $last1, $arr[$i][$x=$x+2]/*virtual platform*/, $d = 1, $last1, $arr[$i][$x=$x+3]/*call rates*/);
$x = $x+4;
$last1++;
}
To add it to the database, I just do a for loop through the array $stmt[]. In theory, when this is executed, I should get something like the following added to the database:
102, Out 1, XML, 102, 102, default, 1, 102, rate1
103, Out 2, HTML, 103, 103, default, 1, 103, rate2
Instead, both rows look like this when they are added to the db:
104, Out 1, XML, 104, 104, default, 104, rate1
104, Out 2, HTML, 104, 104, default, 104, rate2
Any ideas why this is happening? When the statement is finally executed, is it using the final value of last1, instead of the value of last1 when the statement was prepared?
edit3: I dropped $n, the whole thing increments through $i, but that is still not the point of the question.
Upvotes: 0
Views: 52
Reputation: 22656
bind_param
binds parameters to the query when it is executed. Whenever $stmt[$n]
is executed it will use the parameters at that time. After the loop has ran $last1
is the last value and any executions will use this value.
A simpler way is just to loop and execute at the same time.
Upvotes: 1