English 中文(简体)
Php s Mysqli随意(非常奇怪)发言。
原标题:Php s Mysqli executes statements in random order (very strange)

http://www.un.org。 我非常抱歉,我发现错误,非常 st。 (见答复)

我似乎有一个非常奇怪的问题。 我有一个网站,利用第3个政党批准伐木。 我的使用者为此使用两个社会网络:Facebook和Vkontakte(俄罗斯模拟)。

在记录上,我用已经通过的社会补贴(根据选定的社会网络,用户在FB或VK进行id,我有两个不同的栏目)搜寻用户数据库。

如果用户在FB和VK都有账户,并且通过这两个账户都有账簿,那么现在他或她在我的网站上有两个单独的账户。 然而,他可以通过在一个社会网络(这是他的主人账户)上进行伐木,并利用用户——组合功能与另一个社会网络。

该功能发现用户有另一个账户,并将数据库中的所有数据与总账户重新链接。 然后,它删除了另一个账户,并给总账户增加社会补贴,以便现在用户能够通过两个社会网络进行记录。 社会救助一栏中自然有联合国协会的要求指数。

然而,当该书执行时,似乎会执行《世界人权宣言》,该声明增加了社会补贴, ,删除了老用户。 这产生了错误,因为它试图增加现有的社会补贴(因为旧的使用者仍然在那里)。

当我对数据库进行文字执行后检查时,老用户就去了,因此我猜测,这意味着DELETE声明确实得到了执行,但拖延了,其他陈述得到了执行。 MySQL Workbench的标识证实了这一点,尽管我不敢确定它是否可靠。

My question is how do I ensure that the DELETE (or any other MySQL statement for that matter) has actually been executed before executing the rest of the script? And why does this happen anyway?

这里是经过充分修订的法典(尽管我很乐意接受一个没有守则的答案,只是解释这一原则)。

The user_bind function:

function user_bind($eSourceType)
    {
    //$eSourceType can be  fb  or  vk , depending on the social network of the secondary account

    $usrMe=get_gl_me(); //gets the user s account, through which he is logged in - the master account
    if ($eSourceType== fb ) //if the social network that we are binding this account to is Facebook
        {
        $vSidName= facebook_id ; //name of the column which contains the social id
        if (!$usrMe->get_private_property("facebook_id") & $usrMe->get_private_property("vkontakte_id") ) //check if the master account really doesn t have facebook_id set
            {
            $fb=get_facebook();//gets facebook object (from FB PHP SDK)
            $sid=$fb->getUser();//gets user s id in facebook (social id)
            }
        else
            {
            error("The account has facebook_id set");
            }
        }
    elseif($eSourceType== vk )//same as before, but the id is fetched through $_GET, not object
        {
        $vSidName= vkontakte_id ;
        if ($usrMe->get_private_property("facebook_id") & !$usrMe->get_private_property("vkontakte_id") ) //check if it s the right account
            {
            $sid=$_GET[ uid ];
            }
        else
            {
            error("The account has vkontakte_id set");
            }
        }

    if(!$sid) //if we couldn t retrieve the social id
        {
        error("Can t bind: $sid not set.");
        }

    $idNew=$usrMe->get_id();//get id (database id) of the master account

    $usrOld=fetch_user_by_sid($sid, $eSourceType, true); //fetches the  user  object by the social id we retrieved before
    if ($usrOld)//if there is a user with this social id (if there is a secondary account)
        {
        $idOld=$usrOld->get_id();//get id of the secondary account
        $tblsRelink=array("comments", "posts", "users_private", "vote_posts", "vote_comments"); //get tables in which we have to relink users
        foreach($tblsRelink as $tbl)
            {
            //update set users_idusers to userid
            $sp=new Statement_Parameter; //this is a class from PHP.com: http://php.net/manual/en/mysqli-stmt.bind-param.php. It allows to bind variables to the prepared statement in MySQLi without much pain
            $query="UPDATE $tbl SET users_idusers=" . db_stmt_operands($idNew, $sp,  idNew ) . " WHERE users_idusers=". db_stmt_operands($idOld, $sp,  idOld ); //db_stmt_operands inserts question marks in the query, while binding the variables through Statement_Parameter
            $affected_rows=db_submit($query, $sp);//see below for the db_submit() function explanation
            }

        //delete old user
        $sp=new Statement_Parameter; //clear Statement_Parameter

        $query="DELETE FROM users WHERE idusers=" . db_stmt_operands($idOld, $sp,  idOld );
        $affected_rows=db_submit($query, $sp);
        echo "<br>affected: $affected_rows<BR>"; //this actually returns 1

        //lets see if the user was actually deleted
        $usrTest=fetch_user_by_sid($sid, $eSourceType, true); //fetch the user by the social id
        if($usrTest) //if a user is fetched
            {
            debug_array($usrTest); //custom implementation of print_r
            error("User still exsists. Oh no.");//it always does
            }
        }


    $usrMe->set_private_property($vSidName, $sid);//sets the property  facebook_id  or  vkontakte_id  to the social id that we got in the beginning
    $usrMe->update();//UPDATE statement, which brings the object s properties in the database up to date (in our case: adds the social id)
    //the UPDATE statement doesn t execute because the old user is still there
    }

db - 提交功能:

function db_submit($query, $sp=NULL)
    {
    $mysqli = db_connect(); //fetches PHP MySQLi object
    if ($stmt = $mysqli->prepare($query)) //if the statement is successfully prepared
        {
        if($sp)//if there is a Statement_Parameter passed
            {
            $sp->Bind_Params($stmt); //bind parameters from SP
            }

        if($stmt->execute())//try to execute the statement
            {
            //on success
            if ($mysqli->insert_id) //if this was an INSERT
                {
                return $mysqli->insert_id;
                }
            else //if this was DELETE or UPDATE
                {
                return $mysqli->affected_rows;
                }
            }
        else
            {
            //on failure
            error("Could not submit: could not execute statement. Query: $query." . $stmt->error); //this kills the script
            }
        }
    else
        {
        error("Could not submit. Query: $query." . $mysqli->error); 
        }
    }
最佳回答

事情是:私人企业(包括社会企业)或目标使用者都存放在一个单独的表格(用户——私人)上,通过外国钥匙与主要表格(用户)挂钩。

一包括用户——需要重新链接的表格阵列中的私人表格:

        $tblsRelink=array("comments", "posts", "users_private", "vote_posts", "vote_comments"); 

这使用户——对旧用户的私人——记录与新用户(现在有2个记录——我如何不做这个实地的“UNI Request”)重新链接。 因此,当旧用户被删除时,其相关用户——私人记录不是,因为它现在与新用户有联系。 自然,增加社会补贴的尝试产生了一个错误,因为这一错误已经存在,与旧用户重新挂钩。

This could be prevented by either

  1. Thinking a bit more about what I was doing (why did I consider users_private a table eligible for relinking?)
  2. More careful database structuring (if a field is supposed to be unique - create a UNIQUE key!)

甚至更好。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签