要明确我在发布之前已经查看了堆栈溢出和php.net文档的帮助。
我想要完成的任务:
表之间有FK引用,似乎触发错误的查询是初始查询(父表),它们是分开运行的。全部使用mysqli-> free_result()。
查询在phpmyadmin中有效。
$sqlTPO = "INSERT INTO grc_sandbox_tpos (buyer_name,date_created,item_count,vendor_stid,vendor_name,vendor_address_l1,vendor_address_l2,vendor_city,vendor_state,vendor_zip,vendor_country,vendor_phone)VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
$va = [$bn,$dc,$ic,$vstid,$vn,$vl1,$vl2,$vcity,$vstate,$vzip,$vcntry,$vphn];
$result_tpo = parent::executePreparedWriteingQuery($sqlTPO,$va);
$this->currentTPOId = $result_tpo["insert_id"];
$this->Message = $result_tpo["message"];
$this->Status = $result_tpo["result"];
if ($this->Status == false){
return $this;
}
以下是database.class.php:write query:
protected function executePreparedWriteingQuery($sql, $bavArray)
{
if (!is_string($sql) || empty($sql)) {
return ['result'=>false,"message"=>"SQL statement in an incorrect format or empty.","insert_id"=>null];
}
if ($stmnt = $this->dbConn->prepare($sql)) {
// bind params if they are set
if (!empty($bavArray)) {
$types = '';
foreach ($bavArray as $param) {
// set param type
switch ($param) {
case is_string($param) == true:
$types .= 's'; // strings
break;
case is_int($param) == true:
$types .= 'i'; // integer
break;
case is_float($param) == true:
$types .= 'd'; // double
break;
default:
$types .= 'b'; // default: blob and unknown types
}
}
$bind_names[] = $types;
for ($i = 0; $i < count($bavArray); $i++) {
$bind_name = 'bind' . $i;
$$bind_name = $bavArray[$i];
$bind_names[] = &$$bind_name;
}
call_user_func_array(array($stmnt, 'bind_param'), $bind_names);
}
$result = $stmnt->execute();
if (!$result) {
$msg = $stmnt->error;
$stmnt->free_result();
return ["result"=>false,"message"=>"SQL statement has errors and could not be completed: " . $msg, "insert_id"=>null];
}else{
$id = $this->dbConn->insert_id;
$stmnt->free_result();
return ["result"=>true,"message"=>"Data has been written to the database with out error.","insert_id"=>(substr(strtolower($sql),0,6)=="insert")?$id:null];
}
}else{
$msg = $this->dbConn->error;
return ["result"=>false,"message"=>"(Writing) SQL statement could not be prepared: " . $msg, "insert_id"=>null];
}
}
我在此发现的所有研究都是从数据库读取数据或运行存储过程,我没有使用其中任何一种,但在尝试写入数据库时它仍然失败。
我错过了什么吗?或者其他任何人都有解决方案,因为此时我已经耗尽了我的资源。
答案 0 :(得分:0)
我终于找到了一个可行的解决方案:问题确实是导致hickup的multi_query:所以我提出堆栈溢出顶部答案/评论http://php.net/manual/en/mysqli.multi-query.php
上找到的新的multi_query解决方案 if (!is_string($sql) || empty($sql)) {
return ['result'=>false,"message"=>"SQL statement in an incorrect format or empty.","insert_id"=>null];
}
$result = $this->dbConn->multi_query($sql);
if (!$result) {
return ['result'=>false,"message"=>"SQL statement has errors and could not be completed: " . $this->dbConn->error,"insert_id"=>null];
}else{
$id = $this->dbConn->insert_id;
try{
//use supression to allow completion of action with out folly;
while (@$this->dbConn->next_result()) {;}
}
catch(Exception $e){
//do nothing
}
finally{
return ['result'=>true,"message"=>"SQL ran successfully.","insert_id"=>(substr(strtolower($sql),0,6)=="insert")?$id:null];
}
}
我选择抑制错误,因为此方法仅用于插入而不是读取,它抛出的错误更多的是警告声明使用选择方法需要使用检查是否有更多结果。 ..这是有效的,不需要进一步,我要感谢PatrickQ的投入。