代码之家  ›  专栏  ›  技术社区  ›  MJB

如何防止PHP变量成为数组或对象?

  •  2
  • MJB  · 技术社区  · 14 年前

    认为 那是我的问题。我建立了一个MySQL连接,读取了一个XML文件,然后通过循环遍历元素将这些值插入到表中。问题是,有时我会插入2、3或4,而不是只插入1条记录。这似乎取决于我以前读过的值。我想我正在重新初始化变量,但我想我遗漏了一些东西——希望是一些简单的东西。

    这是我的密码。最初我有大约20个专栏,但是我缩短了包含的版本以便于阅读。

    $ctr = 0;
    $sql =  "insert into csd (id,type,nickname,hostname,username,password) ".
            "values (?,?,?,?,?,?)";
    $cur = $db->prepare($sql);
    
    for ($ctr = 0; $ctr < $expected_count; $ctr++) {
        unset($bind_vars,$dat);
    
        $lbl = "csd_{$ctr}";
    
        $dat['type']      = (string) $ref->itm->csds->$lbl->type;
        $dat['nickname']  = (string) $ref->itm->csds->$lbl->nickname;
        $dat['hostname']  = (string) $ref->itm->csds->$lbl->hostname;
        $dat['username']  = (string) $ref->itm->csds->$lbl->username;
        $dat['password']  = (string) $ref->itm->csds->$lbl->password;
    
        $bind_vars = array( $id,$dat['$type'], $dat['$nickname'], $dat['$hostname'], 
                            $dat['$username'], $dat['$password']);
        print_r ($bind_vars);
        $res = $db->execute($cur, $bind_vars);
    }
    

    另外,我还标记了这个simpleXML,因为这就是我读取文件的方式,尽管上面没有包含该代码。看起来是这样的:

    $ref = simplexml_load_file($file);
    

    更新 :我已经根据建议更改了周围的代码,现在它并不总是相同的模式,但它也同样被破坏。当我在插入之前显示绑定数组时,它看起来像这样。请注意,我还对前后的行进行计数,因此有0行,然后插入1,然后插入2:

    0 CSDs on that ITEM now.
    Array
    (
        [0] => 2
        [1] => 0
        [2] =>
        [3] => X
        [4] => XYZ
        [5] =>
        [6] =>
        [7] =>
        [8] => audio
        [9] =>
        [10] => 192.168.0.50
        [11] => 192.168.0.3
        [12] => 255.255.255.0
        [13] => 255.255.255.0
        [14] =>
        [15] =>
        [16] =>
        [17] => 21
        [18] => 5
        [19] => Y
        [20] => /dir
    )
    2 CSDs on that ITEM now.
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   apis17    14 年前

    不确定确切答案,但这可能有帮助

    $ctr = 0;
    $sql =  "insert into csd (id,type,nickname,hostname,username,password) ".
            "values (?,?,?,?,?,?)";
    $cur = $db->prepare($sql);
    
    for ($ctr = 0; $ctr < $expected_count; $ctr++) {
    
        //list (  $lbl, $type, $nickname, $hostname, $username, $password) = "";
        //$bind_vars = array();
        // use unset
        unset($bind_vars,$dat);
    
        $lbl = "csd_{$ctr}";
    
        $dat['type']      = $ref->itm->csds->$lbl->type;
        $dat['nickname']  = $ref->itm->csds->$lbl->nickname;
        $dat['hostname']  = $ref->itm->csds->$lbl->hostname;
        $dat['username']  = $ref->itm->csds->$lbl->username;
        $dat['password']  = $ref->itm->csds->$lbl->password;
    
        $bind_vars = array($id,$dat['$type'],$dat['$nickname'],$dat['$hostname'],$dat['$username'],$dat['$password']);
        $res = $db->execute($cur, $bind_vars);
    
        # this is a separate function which works, but which only 
        # does SELECTS and cannot be the problem.  I include it because I 
        # want to count the total rows.
    
        printf ("%d CSDs on that ITEM now.\n",  CountCSDs($id_to_sync));
    }
    

    或者你应该检查/回音 $expected_count 首先确定正确计数的值

        2
  •  0
  •   MJB    14 年前

    原来这个脚本是由cron作业运行的,但是有人用另一个cron作业运行我的脚本,因此它有时创建的记录是原来的两倍,有时不是。我看得越多,为解决问题而创建的代码越多,越慢,越糟糕。

    换句话说,当我不看它的时候,它比我看它的时候工作得更好。

    该死的那些人都想帮我!