简体   繁体   English

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

[英]PHP and SQL - Consult and WHERE clause with variable

I'm actually coding the schedule of some conferences and I'm a little bit derping with the php code. 我实际上是在编写一些会议的日程安排,而我对php代码却有些不了解。

The code is the following: 代码如下:

<?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();
?>

The output of this code is intended to be something like Session: Name of the session. 该代码的输出旨在类似于Session:会话名称。 Title of the paper and author of that session. 该论文的标题和该会议的作者。

Instead I only see the Session and Name of the session (first fetch) but then I can't see the paper part (second fetch). 取而代之的是,我仅看到会话的会话和名称(第一次获取),但是却看不到纸质部分(第二次获取)。

Any help? 有什么帮助吗?

Thank you 谢谢

This bit: 这一点:

     $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

Is incorrect, you are trying to use the result set directly into the query. 不正确,您正在尝试直接将结果集用于查询。 Instead, you need to retrieve that row and use that in your query like: 相反,您需要检索该行并在查询中使用该行,例如:

     $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']."'";

Note: 注意:

Please, don't use mysql_* functions in new code . 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. 而是了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

Try this one 试试这个

$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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM