【问题标题】:Multiple SQL statements in PHPPHP中的多个SQL语句
【发布时间】:2012-08-21 02:59:37
【问题描述】:

只是问是否可以在 php 中执行 sql 语句的 in 1 函数。

我的 sql 真正做的是从我的数据库中的三个表中获取数据。 我目前不确定使用什么方法。内部连接会弹出,但我忘记了正确的语法。所以我在想这个。

$qry_display = "SELECT student_id,
section_id,level,photo,address,father_occupation,father_phone,father_company,mother_occupation,
mother_phone,mother_company 
from tbl_er 
where student_id='$id' AND level="3rd Year"";

上述语句将从 tbl_er 检索信息,这些将是学生的历史记录。

$qry_display = "SELECT fname,
sex,
lname,
mname,
birth_date,
birth_place,
address,
father,
father_degree,
mother,
mother_degree,
from tbl_enroll where student_id='$id'";

上述语句将从 tbl_enroll 中检索部分信息,这些将是学生的核心不可更改数据。第一条语句还有要检索的“section_id”。所以我的想法是检索该值,以便将其用于最后一个 sql。

$qry_display = "SELECT section_name,adviser_id,schoolyr 
from tbl_section 
where student_id='$id' AND section_id='$sid'";

我对如何获取 section_id 的想法是在我要放置的最后一条语句之前。

    $sid= section_id  (I am unsure if this will work.)

这个语句也会触发一个函数中的所有三个语句吗?

        $sql_display = mysql_query($qry_display) or die (mysql_error());

谢谢你,感谢任何反馈。

【问题讨论】:

  • 你可能对mysqli::multi_query感兴趣。
  • @ACJ 我无法查看。
  • 您无法查看 PHP 手册?

标签: php sql


【解决方案1】:

这有很多问题,即使你解决了这些错误,你也可能无法得到你要找的东西,所以让我们先把它写成 1 个带有左连接的连贯查询(顺便说一句很好)。

$qry_display = "SELECT
            a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,
            b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
            c.section_name, c.adviser_id, c.schoolyr
            FROM tbl_er AS a
            LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
            LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
            WHERE a.student_id=".$id." AND a.level='3rd Year'";

最后一次加入不关心 student_id,因为 section/advisor 关系不依赖于学生。

【讨论】:

  • 知道我闻到了内部连接的味道。谢谢,我会立即深入研究。
【解决方案2】:

我似乎记得MySQL 系列函数不能在单个mysql_query 操作中处理多个语句。它会执行第一条语句,然后默默地丢弃下面的语句,即使您将它们包装在事务中。

也就是说,MySQL 系列已被弃用 - you should be using MySQLi 用于 2012 年的新项目。;)

【讨论】:

  • 期待您的回答。不过谢谢。无法使用 MySQLi 并立即学习它,因为我必须在一周内完成它。所以可能正在研究 UNION 或 INNERJOIN。
  • 为什么无法使用 MySQLi?如果你绝对不能,你唯一的选择是连续执行数据库操作。
  • 现在才听说,可能来不及学习,因为我的系统中有报告功能要完成。
  • MySQLi 并不是我所说的困难,但取决于你的系统有多大,你可能是正确的,更换所有东西是一个坏主意。您可能会在这部分使用 MySQLi,然后从应用程序的其余部分逐步淘汰旧的 MySQL 功能。
猜你喜欢
  • 2010-09-25
  • 2017-08-20
  • 2014-08-20
  • 2012-05-05
  • 2019-04-15
  • 2018-09-27
  • 1970-01-01
  • 2017-11-11
相关资源
最近更新 更多