4

I've been reading in every thread in here that is related to this but I always get it wrong.

Please help cause I always get the error

"Notice: Array to string conversion" in line "$address[] = mysql_result($row, 0 );"

below. Please help.

if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_assoc($p_address))
{     
 $address[] = mysql_result($row, 0 );
}  

$all_address = implode(',', $address);

2 Answers 2

3

Change this line

 $address[] = mysql_result($row, 0 );

To this:

 $address[] = $row;

And then to see the keys and values available in the new $address array, you can do something like this:

 print_r($address);

In order to keep implode() functional, do something like this:

for ($i = 0; $i < count($address); $i++) {
  $all_address[] = implode(',', $address[$i]);
}

Final output:

if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_assoc($p_address))
{     
 $address[] = $row;
}

for ($i = 0; $i < count($address); $i++) {
  $all_address[] = implode(',', $address[$i]);
}

// Example for outputting on screen:
foreach ($all_address as $aa) {
  print $aa . "<br/>\n";
}
}

Hope that helps...

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

5 Comments

It gives the same error. Notice: Array to string conversion in C:\xampp\htdocs\admission\main_admin\announce_email.php on line 468 Array,Array,Array
I think you missed a comma in implode(',' $address[$i]). Yet, it has another error: Warning: implode(): Invalid arguments passed in $all_address[] = implode(',' $address[$i]);
OMG. It works. But I have another problem. And it's in the mail function.
Fixed. Regarding I have another problem. And it's in the mail function., I think you'll have to post another question to make sure everything stays on topic, relative to the topic on hand... Ya know?
Yeah that's my plan, to post another question. I think it's a little bit irrelevant in this thread. Thank you.
0

$row is set in every iteration of the while loop. every time it contains a new table record. So you just need to add each record in the address array.

   while($row = mysql_fetch_assoc($p_address))
   {     
      $address[] = $row;
   }  

5 Comments

It gives the same error. Notice: Array to string conversion in C:\xampp\htdocs\admission\main_admin\announce_email.php on line 468 Array,Array,Array
what is the query you are sending? do a var_dump($row) to see what comes
I believe you mean $row[column]. Else the whole resultset-array would be appended to the $adress-array in each iteration.
I am retrieving email address from the database all at once, and send them messages through mail function.
then try $address[] = $row[0] or $row[column name]; depending on your query

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.