Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 准备好的语句中的数字正在变化_Php_Mysql_Mysqli_Prepared Statement - Fatal编程技术网

Php 准备好的语句中的数字正在变化

Php 准备好的语句中的数字正在变化,php,mysql,mysqli,prepared-statement,Php,Mysql,Mysqli,Prepared Statement,我使用for循环创建n个准备好的语句,并在这些语句中增加一个数字。我在查询这些数字的使用位置时遇到问题。Last1是上次运行时使用的最大数字+1,Last2是我们这次将使用的最大数字。以下是循环和查询: $last1 = 102;//these are normally pulled from the db, but I am specifying them here $last2 = 104;//they are being pulled correctly, I already checke

我使用for循环创建n个准备好的语句,并在这些语句中增加一个数字。我在查询这些数字的使用位置时遇到问题。Last1是上次运行时使用的最大数字+1,Last2是我们这次将使用的最大数字。以下是循环和查询:

$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++;
}
相反,将两行添加到数据库时,它们的外观如下所示:

104, Out 1, XML, 104, 104, default, 104, rate1
104, Out 2, HTML, 104, 104, default, 104, rate2
知道为什么会这样吗?当语句最终执行时,它是否使用last1的最终值,而不是在编写语句时使用last1的值


edit3:我删除了$n,整个过程增加到$I,但这仍然不是问题的关键。

bind_param
在执行查询时将参数绑定到查询。无论何时执行
$stmt[$n]
,它都将使用当时的参数。循环运行后,
$last1
是最后一个值,任何执行都将使用此值


一种更简单的方法是同时循环和执行。

您实际在哪里执行这些查询?您只是将值绑定到语句,必须在其他位置执行,在这种情况下,最后一个绑定值就是您要插入的值。另外,请注意,您正在将所有内容分配给索引$n,我说我做了一个for循环,只循环并执行mysqli_stmt_execute($stmt[$I])。增加索引$i而不是$n只是一个输入错误,我修正了。你现在增加$n了吗?这就提高了for()循环的上限,这使它成为一个无限循环一个更相关的子问题。mysqli_stmt_execute($stmt[$i])和mysqli_commit($con)是否需要在同一个函数中,或者我可以用execute调用一个函数,用commit调用下一个函数?
mysqli_commit
只有在autocommit设置为false时才需要调用。如果在所有执行之后调用
mysqli\u commit
,则会将所有插入写入数据库。
104, Out 1, XML, 104, 104, default, 104, rate1
104, Out 2, HTML, 104, 104, default, 104, rate2