Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP查询从mysql表打印值的最佳方法_Php_Mysql - Fatal编程技术网

通过PHP查询从mysql表打印值的最佳方法

通过PHP查询从mysql表打印值的最佳方法,php,mysql,Php,Mysql,在我的页面中,大多数时候,我只需要使用循环打印表中一个字段的值。例如: <?php for($i=1;$i<=mysql_num_rows($result);$i++) { echo $row['name']; $sql1="select industry from table_industry where profid='".$row['prof']."'"; $result1=mysql_query($sql1); $row1=my

在我的页面中,大多数时候,我只需要使用循环打印表中一个字段的值。例如:

 <?php

 for($i=1;$i<=mysql_num_rows($result);$i++)
 {
     echo $row['name'];
     $sql1="select industry from table_industry where profid='".$row['prof']."'";
     $result1=mysql_query($sql1);
     $row1=mysql_fetch_array($result1);
     echo $row1['industry']; ?>
 }
 ?>
SELECT
  table_prof.profid
, table_prof.name
, table_industry.industry 
FROM table_prof
JOIN table_industry USING ( profid )
ORDER BY table_prof.name ASC;
例如,我在表中有5000+条记录,上面的循环将执行5000+次

从表_industry打印行业字段值的最佳方法是什么

是我上面写的代码,还是有一种更快执行的方法

避免在所有5000条记录上循环。使用筛选或分页 更重要的是避免嵌套查询。使用连接的力量。 避免在所有5000条记录上循环。使用筛选或分页 更重要的是避免嵌套查询。使用连接的力量。
按照您的逻辑,我会选择所有信息,将其加载到哈希中,并在哈希中迭代,而不是终止数据库

按照您的逻辑,我会选择所有信息,将其加载到哈希中,并在哈希中迭代,而不是终止数据库

我最好的猜测是这会帮到你

通过join,您可以指示MySQL服务器组合不同的表,并在单个查询中访问它们,例如:

 <?php

 for($i=1;$i<=mysql_num_rows($result);$i++)
 {
     echo $row['name'];
     $sql1="select industry from table_industry where profid='".$row['prof']."'";
     $result1=mysql_query($sql1);
     $row1=mysql_fetch_array($result1);
     echo $row1['industry']; ?>
 }
 ?>
SELECT
  table_prof.profid
, table_prof.name
, table_industry.industry 
FROM table_prof
JOIN table_industry USING ( profid )
ORDER BY table_prof.name ASC;
一般来说,在循环中查询数据库是一个非常糟糕的主意,除非您确切知道为什么要这样做,否则应该避免。在循环中查询可以很容易地使数据库服务器屈服。

我最好的猜测是这将帮助您解决问题

通过join,您可以指示MySQL服务器组合不同的表,并在单个查询中访问它们,例如:

 <?php

 for($i=1;$i<=mysql_num_rows($result);$i++)
 {
     echo $row['name'];
     $sql1="select industry from table_industry where profid='".$row['prof']."'";
     $result1=mysql_query($sql1);
     $row1=mysql_fetch_array($result1);
     echo $row1['industry']; ?>
 }
 ?>
SELECT
  table_prof.profid
, table_prof.name
, table_industry.industry 
FROM table_prof
JOIN table_industry USING ( profid )
ORDER BY table_prof.name ASC;
一般来说,在循环中查询数据库是一个非常糟糕的主意,除非您确切知道为什么要这样做,否则应该避免。在循环中查询可以很容易地使数据库服务器屈服。

使用JOIN

若您只想在行业表中包含那个些具有值的行,那个么sql将被删除

    SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
      SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      LEFT JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
如果您想包含表_prof中的所有值,那么sql将是

    SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
      SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      LEFT JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
我想这将帮助您……

使用JOIN

若您只想在行业表中包含那个些具有值的行,那个么sql将被删除

    SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
      SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      LEFT JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
如果您想包含表_prof中的所有值,那么sql将是

    SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;
      SELECT 
     table_prof.profid 
    , table_prof.name 
    , table_industry.industry  
      FROM table_prof 
      LEFT JOIN table_industry USING ( profid ) 
      ORDER BY table_prof.name ASC;

我认为这将帮助您…

您为什么不在原始SQL中使用联接来同时提取行业数据?请向我们展示外部查询,即生成$result的查询。注意:将mysql_num_行放入临时变量,不要在每次迭代时调用该函数;对于某些版本的库,这会在每个CALL上产生一个额外的MySQL查询,这是您不在原始SQL中同时使用联接来提取行业数据的原因吗?请向我们显示外部查询,即生成$result的查询。注意:将MySQL_num_行放入一个临时变量,不要每次迭代都调用函数;对于某些版本的库,每次调用都会产生额外的MySQL查询