Jump to content

[SOLVED] can't figure out what's wrong in my code... :( sorry for being so vague


Rohlan

Recommended Posts

Here's the code:

 

<?php
include("config.php");
include("opendb.php");



$sql="SELECT lista_emails FROM noticias WHERE id=1"; 
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

echo $row['lista_emails'];

echo "<hr>";
$sql2= "SELECT * FROM subscritores"; 
$result2=mysql_query($sql2);

while($row2 = mysql_fetch_array($result2)) {
$counter = $counter + 1;
$lista = $row['lista_emails'];
$email = $row2['email'];
$pos = strpos($lista, $email);
echo $counter;

if ($pos === false) {
    echo "Enviar para '$email' ";
mysql_query("UPDATE noticias SET lista_emails='".$row['lista_emails']."$email;' WHERE id=1");
} else {
    echo "Não enviar para '$email' ";
}
echo "<br>";
}
include("closedb.php");


?>

 

The problem is the query inside my while loop... it seems to only happen once during the whole loop..

Link to comment
Share on other sites

How many rows are in the subscritores table?

 

It will only loop for how many rows are returned. Start there and make sure there is more than 1 row. Try running the SQL through phpMyAdmin, and see what returns.

Link to comment
Share on other sites

There are 5 rows in that table.

 

The code generates this output when I run it:

1Enviar para 'whatever@com.com'

2Enviar para 'lol@com.com'

3Enviar para 'stff@com.com'

4Enviar para 'sdf@com.com'

5Enviar para 'ggr@com.com'

 

And the query that's supposed to loop will run, but only once, and if I run the script again, it works again but for the next row only... it seems to only run once and only once even though its supposed to be looping..

Link to comment
Share on other sites

I figured it out.

 

I had an extra query that I used to obtain "$row['lista_emails']", I placed that query inside the while loop and it started working properly, obviously that variable needs to be updated each time the loop restarts, but this wasnt happening before because it was outside the loop.

 

Thanks for the time.

Link to comment
Share on other sites

It is running each time is my guess.

 

mysql_query("UPDATE noticias SET lista_emails='".$row['lista_emails']."$email;' WHERE id=1");

 

You are updating the same id each time. My bet is the last email is the one that gets posted. If you want to append (or concat) the emails to that field, you will need to do something a bit different.

 

MySQL Concat

 

mysql_query("UPDATE noticias SET lista_emails=CONCAT(`lista_emails`, '".$row['lista_emails']."$email;') WHERE id=1");

 

See if that was what you were after.

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.