SQL内部Query不提供输出

时间:2015-03-09 05:32:48

标签: php mysql

/ *在$ selected中从java代码中获取数据,该代码成功存储在$ selected中。在查询中使用$ selected但在触发查询后无法获取任何数据。 * /

     $selected = $_GET['selected'];

     $projects=mysql_query("select ProjectName from projects where LobID =
                ( select LobID from lob where LobName like     ".$selected.");");

 if (!$projects) {
     echo "Could not successfully run query ($projects) from DB: " .mysql_error();
    exit;
}

  /* Have included this query in my php page. All the table names are same.Lob       and projects are two different tables n LobID is primary key in Lob & Foreign key in projects. By executing the above query I am not able to fetch the data in $projects.Instead i am getting mesagecould not successfully run query. please help. */

4 个答案:

答案 0 :(得分:0)

查询应该是这样的

$projects=mysql_query("select ProjectName from projects where LobID =
                     (select LobID from lob where LobName like '$selected')");

".$selected.");更改为'$selected'

答案 1 :(得分:0)

试试这个...... 如果你使用喜欢搜索LobName意味着使用%%。它可以返回可能的匹配值kike单个字符

"select ProjectName from projects where LobID =
                ( select LobID from lob where LobName like '%$selected%');"

答案 2 :(得分:0)

我认为这应该是Joker'和Abdullah Nilam'的回答:

$projects=mysql_query("select ProjectName from projects  
where LobID = ( select LobID from lob 
where LobName like '%".$selected."%');");

或联接

$projects = mysql_query("SELECT ProjectName FROM projects
JOIN lob ON projects.LobID=lob.LobID
WHERE LobName LIKE '%" . $selected . "%');");

答案 3 :(得分:0)

  • 您的$selected = $_GET['selected'];变量中包含哪种类型的数据?
  • 子查询返回多少行记录?

如果您的子查询返回更多记录,您可以限制结果数量:

<?php
  
  /**
    ...
  **/

  $selected = $_GET['selected'];

  $projects = mysql_query("SELECT ProjectName FROM projects WHERE (LobID = (
    SELECT LobID FROM lob WHERE LobName LIKE '%$selected%' LIMIT 1))");

?>

如果没有,你可以这样做:

<?php
      
      /**
        ...
      **/

      $selected = $_GET['selected'];

      $projects = mysql_query("SELECT ProjectName FROM projects WHERE (LobID IN (
        SELECT LobID FROM lob WHERE LobName LIKE '%$selected%'))");

    ?>