在PHP中从MySQL提取主键 [英] Extract primary key from MySQL in PHP

查看:320
本文介绍了在PHP中从MySQL提取主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个PHP脚本,我缺乏提取主键,我已经给下面的流程,请帮助我如何可以修改获得主键



我使用MySQL DB,为Joomla工作,我的要求是跟踪任何表上的插入/更新/删除的活动,并将其存储在另一个审计表使用触发器,即我正在审计。 DB的表结构:几个表没有任何PK或自动增加键



我的脚本流程是:



<
  • 我从DB中取出所有表。

  • 我检查表是否有任何触发器。

  • 如果没有找到任何触发器,那么它将创建表的触发器,例如




    • 首先检查表格是否有任何主键(用于为每个更改插入跟踪审核表)

    • 如果它具有主键,则它在创建触发器中进一步使用它。

    • 如果它没有找到任何PK,则它在创建触发器时继续进行,而不在审计中插入任何id表


    现在这里,我的问题是我每次都需要PK我可以记录执行插入/更新/删除的任何特定表的ID,以便进一步我可以使用这个审计跟踪表复制在生产DB ..
    现在我已经提到,我是在某些表中不能使用PK /自动递增,那么我应该怎么做才能得到改变的特定ID?



    请指导我... GEEKS! !!

    解决方案

    如果我理解您的问题,您需要一个唯一的标识符表格行,没有主键,种类的唯一标识符。这是不容易做,只要我看到。其他数据库有唯一的行ID,但mySQL不。您可以 使用每个列的值来尝试和标识该行,但这远不是重复安全的 - 可能有两个或更多行包含完全相同的值。



    这个SO问题中的一些想法:



    MySQL:对于MySQL表中的每条记录都有类似内部记录标识符的内容吗?


    I have created a PHP script and I am lacking to extract the primary key, I have given flow below, please help me in how can i modify to get primary key

    I am using MySQL DB, working for Joomla, My requirement is tracking the activity like insert/update/delete on any table and store it in another audit table using triggers, i.e. I am doing Auditing. DB's table structure: Few tables dont have any PK nor auto increment key

    Flow of my script is :

    1. I fetch out all table from DB.
    2. I check whether the table have any trigger or not.
    3. If yes then it moves to check for next table and so on.
    4. If it does'nt find any trigger then it creates the triggers for the table, such that,

      • it first checks if the table has any primary key or not(for inserting in Tracking audit table for every change made)
      • if it has the primary key then it uses it further in creation of trigger.
      • if it doesnt find any PK then it proceeds further in creating the trigger without inserting any id in audit table

    Now here, My problem is I need the PK every time so that I can record the id of any particular table in which the insert/update/delete is performed, so that further i can use this audit track table to replicate in production DB.. Now as I haave mentioned earlier that I am not available with PK/auto-incremented in some table, then what should I do get the particular id in which change is done?

    please guide me...GEEKS!!!

    解决方案

    If I understand your question right, you need a unique identifier for table rows that have no primary key and no other kind of unique identifier. That's not easy to do as far as I can see. Other databases have unique Row IDs, but mySQL does not. You could use the value of every column to try and identify the row, but that is far from duplicate-safe - there could be two or more rows containing the exact same values. So I'd say, without a unique identifier, this is something that simply cannot be done.

    Some ideas in this SO question:

    MySQL: is there something like an internal record identifier for every record in a MySQL table?

    这篇关于在PHP中从MySQL提取主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆