简体   繁体   English

MYSQL PHP准备语句

[英]MYSQL PHP Prepared Statements

I understand how prepared statements work and I need to insert 2 million rows so in this case using a prepared statement is the best option for me. 我了解预备语句的工作原理,需要插入200万行,因此在这种情况下,使用预备语句对我来说是最佳选择。 My question and problem is I have a compound unique key on row2 and row3 so I can use ON DUPLICATE KEY UPDATE in the prepared statement fine but what if I wish to use a condition and only update if the lastupdated DATE is before the comparison date? 我的问题是我在row2和row3上有一个复合唯一键,因此我可以在准备好的语句中使用ON DUPLICATE KEY UPDATE,但是如果我希望使用条件并且仅在lastupdated DATE在比较日期之前进行更新,该怎么办? Without a prepared statement I can just use a simple IF in the ON DUPLICATE KEY UPDATE but I can not seem to implement it with a prepared statement since the variables are bound and will be set within the loop after the prepared statement is created. 没有准备好的语句,我只能在ON DUPLICATE KEY UPDATE中使用一个简单的IF,但是由于变量已绑定并且将在创建准备好的语句后在循环内设置,因此我似乎无法使用准备好的语句来实现它。

$link = new mysqli(db_host,db_user,db_pass,db_db);
if($link->connect_errno){
echo "Failed to connect to MySQL.";
}

if(!($stmt = $link->prepare("INSERT INTO table(`id`,`lastupdated`,`col1`,`col2`,`col3`,`col4`,`col5`,`col6`,`col7`,`col8`,`col9`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"))){
echo "Prepare failed: (" . $link->errno . ") " . $link->error;
}

values() function could be used in this case, I believe, as it will return the actual value passed via placeholder at query execution. 我相信在这种情况下可以使用values()函数,因为它将返回查询执行时通过占位符传递的实际值。

Or you can simply bind the same value twice, first for the VALUES section and second for the UPDATE 或者您可以将相同的值绑定两次,第一次绑定到VALUES部分,第二次绑定到UPDATE。

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

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