SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strrchr() works on one server, not the other?

    PHP Code:
    function insert($table$fields$values)
        {
               
    $q .= "INSERT INTO `" $table "` (";
           foreach (
    $fields as $field)
                   {
                        
    $q .= "`" $field "`, ";
                    }
                        
    $q str_replace(strrchr($q','), ''$q);
               
    $q .= ") VALUES (";
               foreach (
    $values as $value)
                   {
                        
    $q .= "'" $value "', ";
                    }
               
    $q .= ");"
    When using the function, it returns:
    INSERT INTO `members` (`userid``username``password`) VALUES ('1', 'dan', 'dan', );

    If I use the strrchr function like so:
    PHP Code:
    $string "('value1', 'value2', '2value3', 'value4',);";

    $string str_replace(strrchr($string','), ");"$string); 
    It returns:
    ('value1', 'value2', '2value3', 'value4');
    Which works....

    As you can see my main goal is to get rid of the last comma in a mysql query to make a function for queries.

    If you know an alternative that works as well, though I'm curious why this does not work.

    Thank you.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using str_replace() is going to bite you hard if one of the values actually contains , )


    You also need to escape the values. Otherwise, a value that contains a single quote or backslash will break your syntax. I assume the $fields array is safe and trusted(no user input). Otherwise, you need to rethink your plan.

    implode() is a great way to add a delimiter between array elements.

    PHP Code:
    foreach ($fields as $key => $val) {
        
    $fields[$key] = "`$val`";
    }
    $fields implode(','$fields);

    foreach (
    $values as $key => $val) {
        
    $values[$key] = "'" mysql_real_escape_string($val) . "'";
    }
    $values implode(','$values);

    return 
    "INSERT INTO `$table` ($fields) VALUES ($values)"

  3. #3
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I was under the assumption in a string containing values and always ending with a ",", that strrchr supposedly only finds the last character "," in the string so I would be fine. Thank you for your advice, the code works well, I thought of implode since I've used it before, surprisingly I thought it added it after every field, not understanding it was for an array so it wouldn't add the implode character after the last entry.

    Thanks

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm, actually the worst I can think of is you might lose a comma in the value, which isn't really a big deal. But, there might be some other possibilities in certain character sets.


Bookmarks

Posting Permissions

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