简体   繁体   English

mysql不会在while语句中更新

[英]mysql not updating in a while statement

I am trying to extract two fields from tables accesscode and student the update the table borrowers with the data that i have extracted from the previous tables. 我正在尝试从表访问代码中提取两个字段,并用我从以前的表中提取的数据来更新表借方。

$q=$db->query("SELECT regnum,  accesscode FROM student,student_accesscode WHERE student.id=student_accesscode.studentid");


while($qd=$q->fetch(PDO::FETCH_ASSOC))
{
$access=$qd['accesscode'];
$regnum=$qd['regnum'];  
$q2=$db->exec("UPDATE borrowers SET cardnumber='$access' WHERE cardnumber='$regnum'");
if($q2)
{
echo $access.'          '. $regnum.'<br/>';     
}
else
{
echo'erro....<br/>';
}
}
?>

Appending $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 附加$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); before $db->query() probably gives you an Exception... $db->query()可能给你一个异常之前...

Prior to the $db->setAttribute , you will get this: $db->setAttribute ,您将获得以下信息:

PDO::prepare(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

So, instead of fetch() , use fetchAll() with foreach loop, it will make you less insane. 因此,将fetchAll()foreach循环一起使用,而不是fetch() ,它将使您的精神变得更少。

Ref. 参考 from php.net 来自php.net

For a statement that you need to issue multiple times, prepare a PDOStatement 
object with PDO::prepare() and issue the statement with PDOStatement::execute(). 

From the PHP exec page at http://www.php.net/manual/en/pdo.exec.php 从位于http://www.php.net/manual/zh/pdo.exec.php的PHP exec页面

I don't know if this is the problem, I always use prepare and execute . 我不知道这是否是问题,我总是使用prepareexecute It could just be for performance reasons. 可能只是出于性能原因。 Something to try anyway. 无论如何都可以尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM