需要帮助从MySQL检索一些数据

时间:2014-04-17 10:54:46

标签: php html mysql explode implode

我有一个关于MySQL的数据库,我目前正在使用3个表。 一个包含电子邮件,一个包含类别,一个连接表只有类别ID和与每个类别相关联的电子邮件ID列表,这些电子邮件已被PHP内爆并用" - "
例如:2 - 5 - 4等等 我正在尝试输出该电子邮件列表。首先,我通过邮件从另一个页面带来类别电子邮件,然后我用它来从连接表中获取电子邮件的字符串,并尝试在爆炸该列表后从电子邮件表中打印出电子邮件。但我收到一个错误。 Anny请帮忙吗?

<?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>";                                                                                                                    
?>

现在没有错误行。没有结果显示。
我有一个id为15的表类别,连接表与类别相同的id,并有4个电子邮件,已被内爆#34; - &#34;他们之间。

4 个答案:

答案 0 :(得分:1)

似乎在线下:

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

应该是:

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

您正在通过$numero = Count($novoarray);运行循环。所以你似乎需要改变上面一行。

另一件事:

你在这里取了两次相同的东西:

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)) {  

没有意义。

答案 1 :(得分:1)

你在这里创建了你的作品:

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

但是在这里你总是使用$numero

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

                                                         ^

你确定它应该是这样的,而不是$cont吗? (for循环改变的实际变量)

答案 2 :(得分:0)

从0开始计数,所以:

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

需要更改为:

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

尝试使用它:

<?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>";
?>

您使用

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

两次..

答案 3 :(得分:0)

$ novoarray = explode(&#39; - &#39;,$ rowq [&#39; email&#39;]); $ numero = Count($ novoarray);

尝试用Count($ novoarray)做什么;