How to prevent PHP variables from being arrays?

Posted by MJB on Stack Overflow See other posts from Stack Overflow or by MJB
Published on 2010-06-02T13:09:50Z Indexed on 2010/06/02 13:14 UTC
Read the original article Hit count: 289

Filed under:
|
|

I think that (the title) is the problem I am having. I set up a MySQL connection, I read an XML file, and then I insert those values into a table by looping through the elements. The problem is, instead of inserting only 1 record, sometimes I insert 2 or 3 or 4. It seems to depend on the previous values I have read. I think I am reinitializing the variables, but I guess I am missing something -- hopefully something simple.

Here is my code. I originally had about 20 columns, but I shortened the included version to make it easier to read.

    $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();

        $lbl = "csd_{$ctr}";

        $type      = $ref->itm->csds->$lbl->type;
        $nickname  = $ref->itm->csds->$lbl->nickname;
        $hostname  = $ref->itm->csds->$lbl->hostname;
        $username  = $ref->itm->csds->$lbl->username;
        $password  = $ref->itm->csds->$lbl->password;

        $bind_vars = array($id,$type,$nickname,$hostname,$username,$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));
    }

P.S. I also tagged this SimpleXML because that is how I am reading the file, though that code is not included above. It looks like this:

$Ref = simplexml_load_file($file);

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql