mysqli执行函数如何在内部工作?

时间:2016-06-29 18:11:34

标签: php mysql database mysqli prepared-statement

我有一个数据库,我有一些表,其中大多数与这个问题无关:

Content_Handler的:

+-------------+-------------------+---------------------------+
| app_data_id | app_menu_entry_id | content_item_container_id |
+-------------+-------------------+---------------------------+
| 0           | 0                 | NULL                      |
| 0           | 1                 | bm9zb3Ryb3MtMC0w          |
+-------------+-------------------+---------------------------+

App_menu_entry:

+-------------------+----------+---------+--------------+
| app_menu_entry_id | position | name    | icon_id      |
+-------------------+----------+---------+--------------+
| 0                 | 0        | Nombre  | asd          |
| 1                 | 1        | Precios | icontest.png |
+-------------------+----------+---------+--------------+

Content_item_container:

+---------------------------+--------------+
| content_item_container_id | content_name |
+---------------------------+--------------+
| bm9zb3Ryb3MtMC0w          | Nosotros     |
+---------------------------+--------------+

content_handler.app_menu_entry_id REFERENCES TO app_menu_entry.app_menu_entry_id

content_handler.content_item_container_id REFERENCES TO content_item_container.content_item_container_id

现在,当我想要更新app_menu_entry时,我删除了这个表中的所有记录(使用cascade,所以也从content_handler中删除内容)然后我从数组中插入PHP数据,所以我使用带有这样的预处理语句的foreach:

$menuQuery = $connection->prepare("INSERT INTO app_menu_entry(app_menu_entry_id,position,name,icon_id) VALUES(?,?,?,?)");
$handlerQuery = $connection->prepare("INSERT INTO content_handler(app_data_id, app_menu_entry_id, content_item_container_id) VALUES(?,?,?)");
$id = 0;

if (!$menuQuery || !$handlerQuery) {
    ErrorHandler::setError(ErrorHandler::DB_QUERY_ERROR, true);
    throw new \Exception('Error al realizar la consulta');
}

foreach ($menu['menu_items'] as $menuItem) {
    $name = $menuItem['name'];
    $position = $menuItem['position'];
    $icon = $menuItem['icon_id'];
    $contentId = ($menuItem['content'] != null) ? base64_encode($menuItem['content'] . '-' . $userId . '-' . $appId) : null;

    $menuQuery->bind_param('ssss', $id, $position, $name, $icon);
    $menuQuery->execute();

    $handlerQuery->bind_param('sss', $appId, $id, $contentId);
    $handlerQuery->execute();

    $id++;
}

问题是这个循环在app_menu_entry中插入数据而在content_handler中插入数据。这可以在sleep(1000);函数之间添加execute()来解决,这让我想到如何防止使用sleep()以及此执行函数如何工作?它是否使用线程,制作异步队列......?

0 个答案:

没有答案
相关问题