简体   繁体   English

仅在随后调用相同的 function 时出现 mysqli 错误 - “没有下一个结果集。”

[英]mysqli error only on subsequent calls to same function - 'there is no next result set.'

I am iterating rows of a csv file.我正在迭代 csv 文件的行。

On row 1, I call stored procedure (import_extended_data_sp) and it succeeds.在第 1 行,我调用存储过程 (import_extended_data_sp) 并且它成功了。

On row 2, the call fails with:在第 2 行,调用失败并显示:

Strict Standards mysqli::next_result(): There is no next result set.严格标准 mysqli::next_result():没有下一个结果集。

However, with the call being exactly the same as the first, I am struggling to see why?但是,由于电话与第一个电话完全相同,我很难理解为什么?

I have now hard coded test values as parameters, and checked that the Sproc has no issue with the same values being given twice.我现在将测试值硬编码为参数,并检查 Sproc 是否存在两次给出相同值的问题。

It still fails on the second call??第二次通话还是失败??

Am wondering if there is some nuance of mysqli, where I need to clear or reset something before making the second call?我想知道 mysqli 是否有一些细微差别,我需要在拨打第二个电话之前清除或重置某些内容?

<?php include("cnn.php");?>    
<?php include("fn_db.php");?>

# ... get csv file (skipped for brevity) #

while($row = fgetcsv($file_data))
{
    $line = array_combine($head, $row);
       
    # This call works on every loop - no issues
    $id = placemark_to_db($mysqli,$v_header,$line['id_placemark'],$line['name'],$line['swim_type'],$line['latitude'],$line['longitude'],$line['description']);
        
    # This next line only succeeds on first call, but fails on next while loop
    $x = xtended_to_db($mysqli,'99','[{"xtra":"oo"}]');
} 

** fn_db.php >> xtended_to_db** ** fn_db.php >> xtended_to_db**

function xtended_to_db($cn,$id,$jsonarray){
    # procedure returns a rowcount in output parameter

    $cn->multi_query( "CALL import_extended_data_sp($id,'$jsonarray',@out);select @out as _out");
    $cn->next_result();
    $rs=$cn->store_result();
    $ret = $rs->fetch_object()->_out;
    $rs->free(); 
    return $ret;
}

cnn.php cnn.php

<?php
$mysqli = new mysqli("xxx.xxx.xxx.xxx","mydb","pass","user");
// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
?>

The best way to fix this error is to avoid multi_query() altogether.修复此错误的最佳方法是完全避免multi_query() While it might sound like a reasonable use case with stored procedures, the truth is this function is mostly useless and very dangerous.虽然这听起来像是一个使用存储过程的合理用例,但事实是这个 function 几乎没用而且非常危险。 You can achieve the same result using the normal way with prepared statements.您可以使用准备好的语句的正常方式获得相同的结果。

function xtended_to_db(mysqli $cn, $id, $jsonarray) {
    $stmt = $cn->prepare('CALL import_extended_data_sp(?,?,@out)');
    $stmt->bind_param('ss', $id, $jsonarray);
    $stmt->execute();

    $stmt = $cn->prepare('select @out as _out');
    $stmt->execute();
    $rs = $stmt->get_result();
    return $rs->fetch_object()->_out;
}

If you are stuborn and you want to keep on using multi_query() then you need to be more careful with how you fetch results.如果您很顽固并且想继续使用multi_query() ,那么您需要更加小心获取结果的方式。 This function is extremely difficult to get right.这个 function 是极难做对的。 I am not going to show you how to fix multi_query() as I consider it too dangerous with variable input.我不会向您展示如何修复multi_query() ,因为我认为变量输入太危险了。

One last note, you really should think about getting rid of stored procedures.最后一点,你真的应该考虑摆脱存储过程。 They are cumbersome and offer pretty much no benefit.它们很麻烦,几乎没有任何好处。 There definitely is a better way to achieve what you want rather than calling stored procedure from PHP, but without seeing its contents I can't give you better advice.肯定有更好的方法来实现你想要的,而不是从 PHP 调用存储过程,但是如果没有看到它的内容,我无法给你更好的建议。

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

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