- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试运行以下内容。
<?php
$db = mysqli_connect("localhost","user","pw") or die("Database error");
mysqli_select_db($db, "database");
$agtid = $_POST['level'];
$sql = sprintf("call agent_hier(%d)", $agtid);
$result = mysqli_query($db, $sql) or exit(mysqli_error($db));
if ($result) {
echo "<table border='1'>
<tr><th>id</th>
<th>name</th>
<th>parent_id</th>
<th>parent_name</th>
<th>level</th>
<th>email</th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
$aid = $row["id"];
$sql2 = "SELECT * FROM members WHERE MEMNO = '$aid'";
$result2 = mysqli_query($db,$sql2) or exit(mysqli_error($db));
while ($newArray = mysqli_fetch_array($result2)) {
$fname = $newArray['FNAME'];
$lname = $newArray['LNAME'];
$mi = $newArray['MI'];
$address = $newArray['ADDRESS'];
$city = $newArray['CITY'];
$state = $newArray['STATE'];
$zip = $newArray['ZIP'];
$kdate = $newArray['KDATE'];
$date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24);
}
echo sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$row["id"],$row["name"],
$row["parent_id"],$row["parent_name"],
$row["level"],$row["email"]);
}
echo "</table>";
}
mysqli_free_result($result);
mysqli_close($db);
?>
如果我从以下位置删除行:
$aid = $row["agent_id"];
到....
$date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24);
}
一切都会好起来的。如果没有,我会收到以下错误:
Commands out of sync; you can't run this command now
在研究中,我认为这可能是由于多个 MySQLi 查询同时运行,其中使用 mysqli_multi_query
但对于 the guide 中的所有样本和一般数据似乎不适用。
有什么想法吗?
最佳答案
MySQL 客户端不允许您执行新查询,其中仍有行要从正在进行的查询中获取。见 Commands out of sync在 MySQL 文档中关于常见错误。
您可以使用 mysqli_store_result()
从外部查询中预取所有行。这将在 MySQL 客户端中缓冲它们,因此从服务器的角度来看,您的应用程序已经获取了完整的结果集。然后,即使在从现在缓冲的外部结果集中获取行的循环中,您也可以执行更多查询。
或者你mysqli_result::fetch_all()
它将完整的结果集作为 PHP 数组返回,然后您可以循环该数组。
调用存储过程是一种特殊情况,因为存储过程有可能返回多个结果集,每个结果集都可能有自己的行集。这就是@a1ex07 的答案提到使用 mysqli_multi_query()
并循环直到 mysqli_next_result()
没有更多结果集的原因。这是满足 MySQL 协议(protocol)所必需的,即使在您的情况下,您的存储过程只有一个结果集。
PS:顺便说一句,我看到您正在执行嵌套查询,因为您有表示层次结构的数据。您可能需要考虑以不同的方式存储数据,以便更轻松地查询它。我做了一个题为 Models for Hierarchical Data with SQL and PHP 的演讲。 .我还在我的书 SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming 的一章中讨论了这个主题.
下面是如何在CodeIgnitor 3.0.3中实现mysqli_next_result()
:
system/database/drivers/mysqli/mysqli_driver.php
第262行修改
protected function _execute($sql)
{
return $this->conn_id->query($this->_prep_query($sql));
}
到这里
protected function _execute($sql)
{
$results = $this->conn_id->query($this->_prep_query($sql));
@mysqli_next_result($this->conn_id); // Fix 'command out of sync' error
return $results;
}
这是自 2.x 以来一直存在的问题。我刚刚更新到 3.x,不得不将这个 hack 复制到新版本。
关于php - 为什么 mysqli 给出 "Commands out of sync"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632075/
是否可以使用 MS Sync Framework 同步不同数据库中的不同表(具有完全不同的结构),假设其中一个表具有主键,另一个具有唯一索引,其值与第一个表的 PK 一致(但不能晋级PK)?列名也可以
我正在开发一个使用 Twilio Sync 作为数据库的应用程序。我确实订阅了很多事件,例如 itemAdded、itemUpdated、itemRemoved,以使用 Sync Javascript
我们计划使用 ADO.NET 的同步服务将商店与总部同步,有人建议我通过 进行同步。 WCF 代理 . 通过 WCF 代理同步是否意味着我将能够 通过 http 同步 ?如果没有,是否可以使用同步服务
在 Sync Framework 2.1 中,批处理似乎得到了内置支持,因此在某些提供程序中,我们可以只提及批处理大小和假脱机文件位置(以及一些其他属性),批处理将得到处理。开发人员不必编写自己的批处
'sync' 事件和 Backbone.sync 有什么区别? ...它们分别是什么? 最佳答案 虽然两者都与向服务器同步数据的任务有关,但两者之间没有任何联系。 Backbone.sync实现您的
在 Go 中,我们可以使用: type Data struct { lock *sync.Mutex } 或 type Data struct { lock sync.Mutex
我们在各种客户项目中使用 Microsoft Sync Framework 2.1 进行文件和数据库同步。扩展支持将于 2021 年结束。 所以我的问题是: 微软的同步框架策略是什么?它会是开源的,所
我在这个站点和其他站点上看到了一些帖子,指出问题通常是由将表添加到客户端的 SyncAgent 配置或提供者端的 SyncAdapter 的顺序引起的。我相信我在这两个地方都正确地对表格进行了排序(我
Using an Asynchronous Controller in ASP.NET MVC 的使用 BeginMethod/EndMethod 模式部分指的是 Sync() 方法。它没有链接,我无
这是代码: type someThing struct { sync.Mutex cv *sync.Cond num int } func NewSometh
异步地做多项工作(排序向量,主要是函数计算(计算是计算或内存限制,目前,我可以用以下方式编写这些操作: 使用 Threads.@spawn _f1 = Threads.@spawn f1(x) _f2
我想使用 Rust 和 once_cell实现一些静态常量结构实例,一个静态常量向量包含这些静态结构实例。 示例代码如下: use once_cell::sync::Lazy; pub struct
我想使用 Rust 和 once_cell实现一些静态常量结构实例,一个静态常量向量包含这些静态结构实例。 示例代码如下: use once_cell::sync::Lazy; pub struct
看了sync.Pool的设计,发现是两个逻辑,为什么要用localPool来解决锁竞争。我们可以只使用 chan 来实现一个。 使用 channel 比 sync.pool 快 4 倍! pool除了
我有一个编码为在模糊时运行 save_notes 函数的文本区域。当 save_notes 调用时,我在控制台中得到以下内容: TypeError: Cannot read property 'syn
如果 Waitgroups 和 Mutex 总是需要通过引用传递,我们不能把它做成引用类型(禁止使用它们作为值传递)吗?我的意思是有没有我们需要使用它们按值传递的用例? 最佳答案 当您将任何参数作为值
背景:我正在尝试创建一个完全异步 Node js 演示的小测试。然后我可以看到一个好的样本是什么样的,以便清理我完成的其他一些项目。 这是我的代码 //https://medium.com/@tkss
http://msdn.microsoft.com/en-us/library/dd918848.aspx “了解作用域是表和筛选器的组合很重要。例如,您可以定义一个名为 sales-WA 的筛选作用
我尝试同步我的 Visual Studio 代码设置/扩展。我手动执行的许多同步步骤。 然后我发现了这两个工具 https://code.visualstudio.com/docs/editor/se
我正在尝试移植一个扩展,但我的 JS 非常生疏。 https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chro
我是一名优秀的程序员,十分优秀!