Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli_multi_query()在使用MySQL变量时出现问题_Php_Mysql_Sql - Fatal编程技术网

Php mysqli_multi_query()在使用MySQL变量时出现问题

Php mysqli_multi_query()在使用MySQL变量时出现问题,php,mysql,sql,Php,Mysql,Sql,我在php脚本中使用了一个mysqli\u multi\u query()。查询如下: SET@firstRow=1; 及 选择组_CONCAT(CONCAT)(如果(@firstRow=1,CONCAT(“亲爱的”), 如果(@firstRow:=0,“,”),“亲爱的”), 家长。问候语,“,家长。姓氏) 分隔符“,”)作为称呼语 从亲子关系、父母 其中parents.ID=parentChildRelation.ParentID 和parentChildRelation.ChildID=

我在php脚本中使用了一个
mysqli\u multi\u query()
。查询如下:

SET@firstRow=1;

选择组_CONCAT(CONCAT)(如果(@firstRow=1,CONCAT(“亲爱的”),
如果(@firstRow:=0,“,”),“亲爱的”),
家长。问候语,“,家长。姓氏)
分隔符“,”)作为称呼语
从亲子关系、父母
其中parents.ID=parentChildRelation.ParentID
和parentChildRelation.ChildID=$ChildID
我知道这个查询很混乱,但这是我让它工作的唯一方法(也许有人对此有想法,尽管这不是这个问题的目的)

我的问题是,
mysqli\u next\u result()
无法正常工作,或者这些查询在
mysqli\u multi\u查询()
中无法正常工作(它在phpMyAdmin中工作…)。或者,很可能是我犯了一个错误

我的php脚本中的相关代码是:

mysqli_multi_query($connection, $queries);
$result = mysqli_store_result($connection);
for($i = 0 ; $i < $queryNumber ; $i++) {
  mysqli_next_result($connection);
}
if($result) {
  while($row = mysqli_fetch_array($result)) {
    print($row[$varName] . "\n");
  }
}
mysqli\u多重查询($connection,$querys); $result=mysqli\u store\u result($connection); 对于($i=0;$i<$queryNumber;$i++){ mysqli_下一个结果($connection); } 如果($结果){ while($row=mysqli\u fetch\u数组($result)){ 打印($row[$varName]。“\n”); } } 在玩一些
var\u dump
时,我注意到结果是
false


同样重要的是:当$querys实际上只是一个查询时,代码运行良好。

对我来说,它看起来像一个经典。就我所理解的代码而言,您想要的结果是“亲爱的史密斯先生,亲爱的史密斯夫人”(示例:)。
@firstRow
的目的是用大写字母“D”写第一个“亲爱的”

嗯。。简化的查询

选择组\u CONCAT(CONCAT(“亲爱的”,parents.hallation,”,parents.Lastname)
分隔符“,”)作为称呼语
从亲子关系、父母
其中parents.ID=parentChildRelation.ParentID
和parentChildRelation.ChildID=$ChildID
将返回类似“亲爱的史密斯先生,亲爱的史密斯夫人”的内容(示例:)

现在您可以只使用PHPs,它将返回“第一个字符str大写的字符串”(示例:)

如果只想使用SQL获得结果,可以将第一个“亲爱的”从
组中移出,并使用
,亲爱的
作为
分隔符

选择CONCAT(“亲爱的”,组_CONCAT(CONCAT(parents.hallation),”,parents.Lastname)
“亲爱的”)作为问候
从亲子关系、父母
其中parents.ID=parentChildRelation.ParentID
和parentChildRelation.ChildID=$ChildID
例如:

另一种避免
设置@firstRow=1的方法行用于在联接的派生表中设置
@firstRow
变量(
(选择@firstRow:=1)
):

选择组_CONCAT(CONCAT)(如果(@firstRow=1,CONCAT(“亲爱的”),
如果(@firstRow:=0,“,”),“亲爱的”),
家长。问候语,“,家长。姓氏)
分隔符“,”)作为称呼语
从parentChildRelation的parents中,(选择@firstRow:=1)initFirstRow
其中parents.ID=parentChildRelation.ParentID
和parentChildRelation.ChildID=$ChildID
例如:

如您所见,有多种方法可以避免使用
@firstRow
变量,甚至还有更多方法可以避免使用
mysqli\u multi\u query()

然而,我不会使用这些解决方案中的任何一种。我要做的是执行一个简单的查询

选择父母。称呼,父母。姓氏
亲子关系
在parents.ID=parentChildRelation.ParentID上加入父对象
其中parentChildRelation.ChildID=?

并在PHP中构建“称呼”字符串。如果您使用(
foreach
)循环或类似于
array\u map
的东西,这没有多大关系。最重要的是,您、您的同事和任何审阅者都可以快速阅读和理解您的代码,并轻松进行必要的更改。因此,您应该花时间找到一个简单的解决方案。这可能会在将来为您节省更多时间。

第一次查询有什么原因吗?这是简化版吗?或者它实际上是一个单行
SET
命令?为什么不把它作为PHP的参数传递呢?是的,只有一行。谢谢!通常,修复PHP脚本中的字符串当然是最好的解决方案。在本例中,我实际上是从另一个表中选择查询。我这样做是为了用查询结果替换字符串中的关键字,这就是为什么查询结果必须是一行中只有一个(相关)列的完成字符串。但其他查询实际上应该可以工作。再次感谢!