Search code examples
phphtmlmysqlexplodeimplode

Need help retrieving some data from MySQL


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.


Solution

  • 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..