Jump to content

problem integrating mysql in an array if more than one element


thedevilinu

Recommended Posts

Hi

I am trying to modify my database and am using the following part of code which isn't working. $bad is various values seperated by a newline. The strange thing about the code is that it works when their is only one $value but won't work if their are two or more values even though it is too easy to be wrong. Please help

 

$badarray=explode(array("\r\n", "\r", "\n"),$bad);

foreach($badarray as $value){

$value= str_replace(array("\r\n", "\r", "\n"),NULL,trim($value));

echo $value;

$query="Update googlevacancies_junk SET exist='$no' WHERE email='$value' ";

mysql_query($query);

}

 

Link to comment
Share on other sites

Try this:

 

$badarray=explode(array("\r\n", "\r", "\n"),$bad);
   foreach($badarray as $value){
      $value= str_replace(array("\r\n", "\r", "\n"),NULL,trim($value));
      echo $value;
      $query="Update googlevacancies_junk SET exist='$no'    WHERE email='$value' ";
      mysql_query($query) or die("Error in query:<br>$query<br>" . mysql_error());   
   }

 

The only change is to check for errors from mysql_query()

Link to comment
Share on other sites

No error came when I put that. However, I noticed something funny.

When $bad is insterted from textarea seperated by a newline in the form of

1@a.com

2@b.com

..................

and then $badarray is formed by the above code, it doesn't work. It will only work if I have one value there. However if I manually define

$badarray as

$badarray=array('1@a.com','2@b.com'.......)

IT WORKS :o

Output of echo $view, however, remains same in both case

Link to comment
Share on other sites

Can you please explain "explode() does not take an array as the first argument"?

 

This is the text that I found in php.net on explode function

"Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter. "

 

There is also an example

pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";

$pieces = explode(" ", $pizza);

echo $pieces[0]; // piece1

echo $pieces[1]; // piece2

 

The only difference in his pizza and mine is that it's pieces are seperated by a space and mine were seperated by a newline.

Moreover, the funny thing is that echo $value works exacly same for $badarray in both cases.

This is the first time I have faced a problem with explode. I use it very often to split things in to array including passing variables in the url for SEO purposes

Link to comment
Share on other sites

Ah!! You are right about that explode and array thing.. however here's a brilliant function i picked up which does exactly what I wanted to

 

function multiexplode($delimiters,$string)

{

    $return_array = Array($string); // The array to return

    $d_count = 0;

    while (isset($delimiters[$d_count])) // Loop to loop through all delimiters

    {

        $new_return_array = Array();

        foreach($return_array as $el_to_split) // Explode all returned elements by the next delimiter

        {

            $put_in_new_return_array = explode($delimiters[$d_count],$el_to_split);

            foreach($put_in_new_return_array as $substr) // Put all the exploded elements in array to return

            {

                $new_return_array[] = $substr;

            }

        }

        $return_array = $new_return_array; // Replace the previous return array by the next version

        $d_count++;

    }

    return $return_array; // Return the exploded elements

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.