www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] More Efficient?

  1. #1
    Join Date
    Oct 2006
    Posts
    1,297

    resolved [RESOLVED] More Efficient?

    The following works perfectly. But, I'd like to know if there is a more efficient way to do it -- because I've got another object, with 24 properties, that needs to be loaded into a MySQL table right after this one. Thanks.
    PHP Code:
    for ($x=0$x<count($res['Identities']); ++$x):
        
    $full_name     mysql_escape_string($res['Identities'][$x]->full_name);
        
    $email_address mysql_escape_string($res['Identities'][$x]->email_address);
        
    $reply_to      mysql_escape_string($res['Identities'][$x]->reply_to);
        
    $signature     mysql_escape_string($res['Identities'][$x]->signature);
        
    $use_sig       mysql_escape_string($res['Identities'][$x]->use_sig);
        
    $sig_reply     mysql_escape_string($res['Identities'][$x]->sig_reply);
        
    $sig_forward   mysql_escape_string($res['Identities'][$x]->sig_forward);
        
    $sql "Insert Into `user_identities` (`userid`, `full_name`, `email_address`, `reply_to`, `signature`, `use_sig`, `sig_reply`, `sig_forward`)
                Values(
    {$userid}, '{$full_name}', '{$email_address}', '{$reply_to}', '{$signature}', '{$use_sig}', '{$sig_reply}', '{$sig_forward}');";
        if (!
    $result mysql_query($sql)):
            echo 
    '<p>Error: ' .mysql_errno(). '; ' .mysql_error(). "<br>\n".
                 
    "sql = \"{$sql}\"</p>\n";
            exit;
        endif;
    endfor; 
    Last edited by so_is_this; 11-28-2006 at 07:05 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    Guayaquil - Ecuador
    Posts
    144
    PHP Code:


    foreach ($res['Identities'] as $x) {
        
    $full_name     mysql_escape_string($x->full_name);
        
    $email_address mysql_escape_string($x->email_address);
        
    $reply_to      mysql_escape_string($x->reply_to);
        
    $signature     mysql_escape_string($x->signature);
        
    $use_sig       mysql_escape_string($x->use_sig);
        
    $sig_reply     mysql_escape_string($x->sig_reply);
        
    $sig_forward   mysql_escape_string($x->sig_forward);
        
    $sql "Insert Into `user_identities` (`userid`, `full_name`, `email_address`, `reply_to`, `signature`, `use_sig`, `sig_reply`, `sig_forward`)
                Values(
    {$userid}, '{$full_name}', '{$email_address}', '{$reply_to}', '{$signature}', '{$use_sig}', '{$sig_reply}', '{$sig_forward}');";
        if (!
    mysql_query($sql)) {
            echo 
    '<p>Error: ' .mysql_errno(). '; ' .mysql_error(). "<br>\nsql = \"{$sql}\"</p>\n";
        }

    Last edited by delr2691; 11-28-2006 at 07:15 PM.

  3. #3
    Join Date
    Oct 2006
    Posts
    1,297
    Yeah, that much I already figured. I was hoping for more.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    PHP Code:
    // create an array of value lists:
    $values = array();
    foreach (
    $res['Identities'] as $x
    {
       
    $values[] = sprintf("($userid,'%','%','%','%','%','%','%')",
                           
    mysql_escape_string($x->full_name),
                           
    mysql_escape_string($x->email_address),
                           
    mysql_escape_string($x->reply_to),
                           
    mysql_escape_string($x->signature),
                           
    mysql_escape_string($x->use_sig),
                           
    mysql_escape_string($x->sig_reply),
                           
    mysql_escape_string($x->sig_forward));
    }
    // now you can do just one insert query with multiple value lists:
    $sql "Insert Into `user_identities` (`userid`, `full_name`, `email_address`, `reply_to`, `signature`, `use_sig`, `sig_reply`, `sig_forward`) VALUES ".implode(', '$values);
    $result mysql_query($sql) or die(mysql_error() . ": $sql"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2006
    Posts
    1,297
    Thanks, NogDog, but I was really also wanting to avoid hard-coding a bunch of property and column names, too.
    So, this is what I've come up with -- and it works. Reactions?
    PHP Code:
    foreach ($res['Identities'] as $obj):
        
    object_to_field_list($obj$fields=''$values='');
        
    $sql "Insert Into `user_identities` (`userid`,{$fields}) Values({$userid},{$values});";
        if (!
    $result execute_sql($sql)) exit;
    endforeach; 
    PHP Code:
    function object_to_field_list(&$obj, &$field_list, &$values_list)
    {
        foreach(
    $obj as $key => $value):
            if (
    strlen($field_list) > 0):
                
    $field_list .= ',';
                
    $values_list .= ',';
            endif;
            
    $field_list .= '`' .$key'`';
            if (
    is_numeric($value)):
                
    $values_list .= $value;
            else:
                
    $values_list .= "'" .mysql_escape_string($value). "'";
            endif;
        endforeach;
    }
    function 
    report_mysql_error($sql)
    {
        echo 
    '<p>Error: ' .mysql_errno(). '; ' .mysql_error(). "<br />\n".
             
    "sql = \"{$sql}\"</p>\n";
    }
    function 
    execute_sql($sql)
    {
        if (!
    $result mysql_query($sql)):
            
    report_mysql_error($sql);
            return 
    false;
        endif;
        return 
    $result;


  6. #6
    Join Date
    Nov 2006
    Posts
    41
    Run it through a benchmark.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles