PHP和SQL - 带变量的Consult和WHERE子句

时间:2014-04-13 11:03:03

标签: php sql variables where fetch

我实际编写了一些会议的日程安排,而且我对php代码有点兴奋。

代码如下:

<?php
mysql_connect("localhost","root","root");
mysql_select_db("pbeusr3");

$sql="SELECT id_s, sessionname FROM sessions";


$result=mysql_query($sql);
$i=0;
if($result!=NULL)
{
    if(mysql_num_rows($result)>0)
    {
        while($row=mysql_fetch_array($result))
        {
           //here you can work with the results...
             echo "Session ".$row['id_s'].":" .$row['sessionname'];
             echo "<br>";
             $a = $row['id_s'];

             $sql2="SELECT id_p FROM presentations WHERE id_s='$a'";
             $result2 = mysql_query($sql2);
             $sql3="SELECT title, author1 FROM papers WHERE id_p='$result2'";
             $result3 = mysql_query($sql3);

             while($row2=  mysql_fetch_array($result3))
             {
                 echo "Title ".$row2['title']. " Author: " .$row2['author1'];
                 echo "<br>";
             }

         }
    }
    else
    {
    }
    mysql_free_result($result);
}

mysql_close();
?>

此代码的输出类似于Session:会话名称。该论文的标题和该会议的作者。

相反,我只看到会话的会话和名称(第一次获取),但后来我看不到纸质部分(第二次获取)。

任何帮助?

谢谢

2 个答案:

答案 0 :(得分:0)

这一位:

     $sql2="SELECT id_p FROM presentations WHERE id_s='$a'";
     $result2 = mysql_query($sql2);
     $sql3="SELECT title, author1 FROM papers WHERE id_p='$result2'"; #<-- here

不正确,您试图将结果集直接用于查询。相反,您需要检索该行并在查询中使用它,如:

     $sql2="SELECT id_p FROM presentations WHERE id_s='$a'";
     $result2 = mysql_query($sql2);
     //fetch the row from the result set
     $row_id_p = mysql_fetch_assoc($result2);
     $sql3="SELECT title, author1 FROM papers WHERE id_p='".$row_id_p['id_p']."'";

注意:

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

试试这个     

$sql="SELECT id_s, sessionname FROM sessions";
$result=mysql_query($sql);
$i=0;

    if(!empty($result))
    {
        while($row=mysql_fetch_array($result))
        {
           //here you can work with the results...
             echo "Session ".$row['id_s'].":" .$row['sessionname'];
             echo "<br>";
             $a = $row['id_s'];

             $sql2="SELECT id_p FROM presentations WHERE id_s='$a'";
             $result2 = mysql_query($sql2);
             if(!empty($result2))
             { 
                 $rowselect=  mysql_fetch_array($result2)
                 $b = $rowselect['id_p']; 
             }else $b='';
             $sql3="SELECT title, author1 FROM papers WHERE id_p='$b' ";
             $result3 = mysql_query($sql3);

             if(!empty($result3))
             {
                 while($row2=  mysql_fetch_array($result3))
                 {
                     echo "Title ".$row2['title']. " Author: " .$row2['author1'];
                     echo "<br>";
                 }
             }
             else
             {
                  //else statement if needed
             } 
             echo "<br>";

         }
    }
    else
    {
         //else statement
    }
  mysql_free_result($result);


mysql_close();
?>