0

I have the following code:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
    {
        for ($i=0; $i<count($row); $i++)
        {
            (DO THING HERE)
            $row[$i] = str_replace("\n", " ", $row[$i]);
            $row[$i] = str_replace("\r", " ", $row[$i]);
        }

    }

I basically want to do, if the associative array key is equal to "email" (so $row['email']) then append "@gmail.com" to it.

0

4 Answers 4

5

See the MYSQL_NUM you have there? That is going to return your data using the column indexes as keys (ie 0, 1, 2, etc).

You must either

a) find out which column index the email field is and do:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
{
    // 'email' is in column number 5
    $row[5] .='@gmail.com';

    for ($i=0; $i<count($row); $i++)
    {
        $row[$i] = str_replace("\n", " ", $row[$i]);
        $row[$i] = str_replace("\r", " ", $row[$i]);
    }
}

b) OR you can change MYSQL_NUM to MYSQL_ASSOC, and do:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
    $row['email'] .='@gmail.com';

    foreach($row as &$value)
    {
        $value = str_replace("\n", " ", $value);
        $value = str_replace("\r", " ", $value);
    }
}

Note the "&" before $value to make it a reference.

I would do the latter (I prefer foreach to for :)

Sign up to request clarification or add additional context in comments.

3 Comments

way to completely bite my answer bro. i demand half your rep
@theman: this SO "heartbeat" implementation is buggy. I didn't even see your answer while I was writing mine.
crescentfresh, i saw your first comment to mine, its all good, you wrote your response better, i was joking the whole time
5

Use a foreach loop and get both the key and the value for the assoc array.

foreach($row as $key => &$value)
{
    if($key == 'email') $value .= "@gmail.com";
}

Also you should be using mysql_fetch_array($result, MYSQL_ASSOC) if you want an associative array returned.

A more efficient way to append to the email key would be something like:

if(isset($row['email']))
    $row['email'] .= '@gmail.com';

Instead of looping through all the columns.

9 Comments

not only is this inefficient, its also wrong. He passed the MYSQL_NUM constant to it, so there are no associative keys. thanks
mysql_fetch_assoc() also fetches associative arrays
wouldn't you need to get $value by reference if you are going to change it ? i.e. foreach ($row as $key => & $value)
Adam, you can't change $value inside the loop like that - as foreach operates on a copy of the real array. You would have to do $row[$key] .= '@gmail.com'. Cheers
adam peck, i see you edited your answer after realizing how wrong you were. is that a submission of defeat? I am ready for you to apoligize
|
1

Paulo had it almost exactly right:

while ($row = mysql_fetch_assoc($result)) {
    foreach($row as $key => &$value) {
        if($key == 'email') $value .= '@gmail.com';
        $value = str_replace("\n", " ", $value);
        $value = str_replace("\r", " ", $value);
    }
}

Note the "&" in the foreach line. That means you're modifying the value in the $row array, not just a copy of it.

Comments

0

I'm not sure if this is exactly what you want but:

$row['email'].='@gmail.com';

This will append @gmail.com to the email row.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.