PHP: valores múltiples en una matriz

I wonder if you could help me figure this out. I am retrieving values (specifically email) from database, and I want to store it in an array.

My problem is that I don't know how to store multiple values and retrieve them separately so that I can send those address with emails. I tried the code below but it didn't work.

$email = "select *  from student";
if ($p_address = mysql_query($email)) {
    while($row = mysql_fetch_assoc($p_address)) {
        $address = mysql_result($row, 0);
    }
}

The only thing that my code does is to send all of the emails to a single address, and that single address is the first entry in the database. Also note that the number of emails sent to tat particular address corresponds to the number of addresses in the database.

Thanks if you could help.

preguntado el 05 de mayo de 13 a las 17:05

Mira aquí: stackoverflow.com/questions/6962771/… mysql_result se mostrarán . -

4 Respuestas

acaba de hacer

$email = "select *  from student";
if ($p_address = mysql_query($email)) {
    $address = array();  
    while($row = mysql_fetch_assoc($p_address)) {
        $address[] = mysql_result($row, 0);
    }
}
print_r($address);

to use it you can call array values like:

$address[0],$address[1],$address[2],$address[...]

to use all adress in mail function use:

$all_address = implode(',', $address);
mail($all_address, $email_subject, $thankyou);

contestado el 05 de mayo de 13 a las 17:05

Hi, I tried your code but I also get this error: "Warning: mysql_result() expects parameter 1 to be resource, array given on line 65". And line 65 is: " $address[] = mysql_result($row, 0 );". - plátano

I put mysql_error() in line 65 and I get this specific error: "Notice: Array to string conversion in line 65" - plátano

mysql_error depreciated as php5, if there was a mysql error it would show it already, no need to put mysql_error to line 65 - user2314575

Trata

<?php
$email = "select *  from student"; 
if ($p_address=mysql_query($email))
{
  $address = array();
  while($row = mysql_fetch_assoc($p_address))
  { 
     $address[] = $row[fieldname];
  }
  $all_address = implode(',', $address);
}
?>

contestado el 05 de mayo de 13 a las 18:05

Hi, I tried your code but I get this error: "Warning: mysql_result() expects parameter 1 to be resource, array given on line 65". And line 65 is: " $address[] = mysql_result($row, 0 );". - plátano

I put mysql_error() in line 65 and I get this specific error: "Notice: Array to string conversion in line 65" - plátano

Puedes hacer algo como esto:

$addresses = array();
$query = mysql_query("select *  from student");
while($row = mysql_fetch_assoc($query))
{
    $addresses[] = row;
}

foreach ($addresses as $address)
{
    echo $address; // Or do what you want to do with each address
}

contestado el 06 de mayo de 13 a las 13:05

Prueba esta

$email = "select *  from student";
if ($p_address = mysql_query($email))
{
    $address=array();   
    while($row = mysql_fetch_assoc($p_address))
    {
        $address[] = $row['your_column_name'];
    }
}

contestado el 05 de mayo de 13 a las 17:05

Hi. I think this works for me. However, the output it gives is in string form. I need an output that is in an array form. Could you help me? - plátano

just type $address[] = $row; <br /> then each row of the array $address will contain the $row array from your database - Mahmoud Mamdouh

Where do I place the $address[]=$row;? - plátano

Type $row instead of $row['your_column_name']; then u,ll get all database row columns as array -> $row as a column inside array -> $address - Mahmoud Mamdouh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.