Question

i have a database on MySQL, which i am currently working with 3 tables. One with emails, one with categories, and a connecting table which only has category IDs and the list of ID's of emails associated to each category, which have been imploded by PHP and are separated with " - "
For example: 2 - 5 - 4 and such and such.
I am trying to output that list of emails. First i bring the category email from another page by post, and then i use it to fetch the string of emails from the connecting table, and try to print out the emails from the email table after exploding that list. But i am getting an error. Anny help on this please?

<?php
mysql_connect("localhost","root","") or die("problema na conexao");
mysql_select_db("trabalho1");
$idcategoria = $_GET["id"]; 
$querye = "SELECT ID,categoria FROM categoria WHERE ID = '".$idcategoria."'";   

$resultse = mysql_query($querye) or die(mysql_error());                 
    while ($rowe = mysql_fetch_array($resultse))    {
    $categorianome = $rowe['categoria'];
                                                    }

                        echo"<center>";
                    echo "Nome da categoria: ".$categorianome."";
                    echo "<table border='2'>\n";
                    echo"<form>";                           
                    echo "<tr align='center'><td>Data de Criação</td><td>Nome</td><td>Email</td><td>Data da ultima Actualização</td></tr>";                             

$queryq = "SELECT * FROM emailcategoria WHERE categoria = '".$idcategoria."'";  
$resultsq = mysql_query($queryq) or die(mysql_error());                 
    while ($rowq = mysql_fetch_array($resultsq))    {



        $novoarray = explode(' - ',$rowq['email']);
        $numero = Count($novoarray);

        for($cont=0;$cont<$numero;$cont++){

        $query = "SELECT * FROM email WHERE id = '".$novoarray[$cont]."'";  
        $results = mysql_query($query) or die(mysql_error());                   
            while ($row = mysql_fetch_array($results))  {


                    while ($row = mysql_fetch_array($results)) {                            
                                    echo "<tr align='center'>\n";
                                    echo "<td><b></b>".$row['datahora']. "\n</td>";                                             
                                    echo "<td><b></b>".$row['nome']. "\n</td>";                                 
                                    echo "<td><b></b>".$row['email']. "\n</td>";                                    
                                    echo "<td><b></b>".$row['dataactual']. "\n</td></tr>";                                                                                                                                                                      

                                                        }                       
                                                    }

                                                                }

}                                                       
echo "</form>\n";
                        echo "</table>\n";
                        echo"</center>";                                                                                                                    
?>

No error line now. Just no results showing.
And i have the table category with an id of 15, have the connection table with the category as the same id, and have 4 emails, which have been imploded with " - " between them.

Was it helpful?

Solution 3

Counting start from 0, so:

$query = "SELECT * FROM email WHERE id = '".$novoarray[$numero]."'";

Need change to:

$query = "SELECT * FROM email WHERE id = '".$novoarray[$numero-1]."'";

Try to use this:

<?php
mysql_connect("localhost","root","") or die("problema na conexao");
mysql_select_db("trabalho1");
$idcategoria = $_GET["id"];
$querye = "SELECT ID,categoria FROM categoria WHERE ID = '".$idcategoria."'";

$resultse = mysql_query($querye) or die(mysql_error());
while ($rowe = mysql_fetch_array($resultse))    {
    $categorianome = $rowe['categoria'];
}

echo"<center>";
echo "Nome da categoria: ".$categorianome."";
echo "<table border='2'>\n";
echo"<form>";
echo "<tr align='center'><td>Data de Criação</td><td>Nome</td><td>Email</td><td>Data da ultima Actualização</td></tr>";

$queryq = "SELECT * FROM emailcategoria WHERE categoria = '".$idcategoria."'";
$resultsq = mysql_query($queryq) or die(mysql_error());
while ($rowq = mysql_fetch_array($resultsq))    {



    $novoarray = explode(' - ',$rowq['email']);
    $numero = Count($novoarray);

    for($cont=0;$cont<$numero;$cont++){

        $query = "SELECT * FROM email WHERE id = '".$novoarray[$cont]."'";
        $results = mysql_query($query) or die(mysql_error());


            while ($row = mysql_fetch_array($results)) {
                echo "<tr align='center'>\n";
                echo "<td><b></b>".$row['datahora']. "\n</td>";
                echo "<td><b></b>".$row['nome']. "\n</td>";
                echo "<td><b></b>".$row['email']. "\n</td>";
                echo "<td><b></b>".$row['dataactual']. "\n</td></tr>";

            }

    }

}
echo "</form>\n";
echo "</table>\n";
echo"</center>";
?>

You use

while ($rowq = mysql_fetch_array($resultsq))    {

Two times..

OTHER TIPS

You created your for here:

for($cont=0;$cont<$numero;$cont++){

but then here you are using always $numero:

$query = "SELECT * FROM email WHERE id = '".$novoarray[$numero]."'";

                                                         ^

are you sure that's the way it should be and not $cont in there? (The actual variable that's being changed by the for loop)

it seems that below line:

  $query = "SELECT * FROM email WHERE id = '".$novoarray[$numero]."'"; 

should be:

    $query = "SELECT * FROM email WHERE id = '".$novoarray[$cont]."'"; 

you are running a loop through the $numero = Count($novoarray);. So it seems that you need to change above line.

Another thing:

you have fetch twice same thing here:

while ($row = mysql_fetch_array($results))  {

                    echo "<tr align='center'><td>Data de Criação</td><td>Nome</td><td>Email</td><td>Data da ultima Actualização</td></tr>";
                    while ($row = mysql_fetch_array($results)) {  

it is meaningless.

$novoarray = explode(' - ',$rowq['email']); $numero = Count($novoarray);

What are trying to do with Count($novoarray);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top