mysqli, loops, and prepared statements
Ok, I don't use 开发者_运维百科php often enough to remember it when I wander back into it. I'm going a bit crazy with this one. I'll post the code below but a few quick explanations. con.php is a php file that creates the database connection $wkHook. There is no issue at all with the connection, so that is not my problem. Basically, I am selecting data from one table, storing it, and then looping through it. In that loop I am trying to insert the data into another table. You will notice that I am echoing $mid in the loop, this is to let me know that the loop itself is working. And it is working. When I loop through every value for $mid is echoed. The query inside the loop, however, is doing nothing. It doesn't even have the decency to throw an error. It just runs through and the loop as if the query wasn't there.
require('con.php');
$runonce=$wkHook->prepare("select moveId, Max, Hev, Work, Split, Rps from Movement order by movementId asc");
$runonce->execute();
$runonce->store_result();
$runonce->bind_result($mid, $max, $hev, $wrk, $spt, $rps);
$user=1;
while($runonce->fetch())
{
echo $mid."<br>";
$runup=$wkHook->prepare("insert into Entry
(userId, movementId, Max, Hev, Work, Split, Rps)
values
(?, ?, ?, ?, ?, ?, ?)");
$runup->bind_param('iisssss', $user, $mid, $max, $hev, $wrk, $spt, $rps);
$runup->execute();
}
$runup->close();
$runonce->close();
$wkHook->close();
You seem to be initializing your database connection over and over again. Try removing the second, inner require('con.php')
.
MySQL errors aren't going to be spit out to the browser by default, you need to call the mysql_error() or mysqli_error functions to see what went wrong if anything. Add this after your execute call in your loop to see if errors are being reported:
echo mysqli_error() . '<br />';
精彩评论