简体   繁体   中英

SQL inner Query not giving output

/* In $selected m getting data from java code which is successsfully getting stored in $selected. Using $selected in query but cannot get any data after firing the query. */

     $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. */

Query should be like this

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

Change ".$selected."); to '$selected'

Try this... If you use like to search LobName means use %% .Its retrun possible match value kike single character

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

I think it should be the mix of Jocker' and Abdulla Nilam' answer:

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

or make a join

$projects = mysql_query("SELECT ProjectName FROM projects
JOIN lob ON projects.LobID=lob.LobID
WHERE LobName LIKE '%" . $selected . "%');");
  • what type of data contained in your $selected = $_GET['selected']; variable?
  • how many lines of records your subquery returns?

if your subquery returns more records, you can either limit the number of results:

 <?php /** ... **/ $selected = $_GET['selected']; $projects = mysql_query("SELECT ProjectName FROM projects WHERE (LobID = ( SELECT LobID FROM lob WHERE LobName LIKE '%$selected%' LIMIT 1))"); ?> 

if not, you can do like this:

 <?php /** ... **/ $selected = $_GET['selected']; $projects = mysql_query("SELECT ProjectName FROM projects WHERE (LobID IN ( SELECT LobID FROM lob WHERE LobName LIKE '%$selected%'))"); ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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