我有一个包含两列的数据库表' id'和' name'。
然后我有一些测试php代码与mysqli_stmt_fetch
尝试使用mysqli_query
在代码中的不同位置执行mysql查询。显然,在fetch循环中,$ link变量不可用,但是如果我在开始时为它分配相同的对象,它就可以工作。
另外,当我在fetch语句中删除了重新分配时,查询了' baz2'如果它至少迭代一次,即使在fetch语句之后也没有执行,但是如果它没有执行则没有。
这里发生了什么? fetch语句是否对$ link对象执行了某些操作?我应该如何在fetch语句中执行查询?
<?php
$link = mysqli_connect("localhost", "root", "vertrigo", "phpmyadmin");
if (mysqli_connect_errno()) {
printf("Connect failed: ", mysqli_connect_error());
exit();
}
$s = "SELECT * FROM test WHERE name='".$_POST["name"]."'";
if ($stmt = mysqli_prepare($link, $s)) {
$s = "INSERT INTO test VALUES(0, 'baz');";
if (mysqli_query($link, $s))
echo "Insert success before the fetch. ";
else
echo "Didn't insert before the fetch. ";
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name);
while (mysqli_stmt_fetch($stmt)) {
$s = "INSERT INTO test VALUES(0, 'foo');";
if (mysqli_query($link, $s))
echo "Insert success inside fetch without reassignment. ";
else
echo "Didn't insert inside fetch without reassignment. ";
$link = mysqli_connect("localhost", "root", "vertrigo", "phpmyadmin");
if (mysqli_query($link, $s))
echo "Insert success with reassigned link. ";
else
echo "Didn't insert inside fetch. ";
}
$s = "INSERT INTO test VALUES(0, 'baz2');";
if (mysqli_query($link, $s))
echo "Insert success after the fetch. ";
else
echo "Didn't insert after the fetch. ";
}
else
echo "coulnd't prepare";
?>
使用$ _POST [&#39; name&#39;]中的现有名称,此php返回&#34;在获取之前插入成功。没有重新分配就没有插入内部提取。使用重新分配的链接插入成功。获取后插入成功。 &#34;
没有:&#34;在获取之前插入成功。获取后插入成功。 &#34;