Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:03:26+00:00 2026-05-24T05:03:26+00:00

Nesting SQL queries in PHP seems to be a bit of a worry. I

  • 0

Nesting SQL queries in PHP seems to be a bit of a worry.
I needed a SQL query to populate select options in a drupal form and I used the following query.

SELECT data FROM webform_submitted_data WHERE nid = 1124 and cid = 4 and 
data not in (SELECT data FROM webform_submitted_data where nid = 1127 and cid = 11 group by data having COUNT(*) > 5)

This query work perfectly fine in phpMyAdmin, but when i try it out in a PHP code (given below) using db_query, it doesn’t work (nothing is returned).

$array = array();
$sql = db_query("SELECT data FROM webform_submitted_data WHERE nid = 1124 and cid = 4 and data not in (SELECT data FROM webform_submitted_data where nid = 1127 and cid = 11 group by data having COUNT(*) > 5)");
while($row = db_fetch_object($sql)) {
$array[$row->data] = $row->data;
}
return $array;

Could anyone please correct me wrt what is wrong in my query or code ?
Also, is there a way to implement this query in PHP ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:03:28+00:00Added an answer on May 24, 2026 at 5:03 am

    Is it worth point out that the two queries are not the same?

    The first query, from phpMyAdmin, uses a AND NOT EXISTS (...subquery...).

    The second uses and data not in (... subquery ... ).

    Implies different behavior.

    EDIT

    In Drupal 6, the data field in webform_submitted_data is a mediumtext field. Using its content as a array key may not be a good idea. How about changing your query like this:

    $array = array();
    $sql = db_query("SELECT data FROM webform_submitted_data WHERE nid = 1124 and cid = 4    and data not in (SELECT data FROM webform_submitted_data where nid = 1127 and cid = 11   group by data having COUNT(*) > 5)");
    while($row = db_fetch_object($sql)) {
        $array[] = $row->data;  // <<<<----- updated array push
    }
    return $array;
    

    This way, you still get an array of results, but without having to use a long string value as key and possibly overwriting duplicates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that (strict) html doesn't allow nesting any non-inline elements inside a <p>
For the longest time, I've been using the following basic formatting for SQL queries
A rather complicated SQL query I was working on got me thinking about a
I have the following query: SELECT * FROM ( SELECT * FROM View_ExecutiveForecastReport WHERE
Is it possible to write a PL/SQL query to identify a complete list of
I am having a problem with nesting lists in IE 7. It seems that
I am trying to write a SQL query where I am joining two tables
Why would PHP allow nesting functions? <?php function foo() { function bar() { return
I have a View Model that has some serious nesting. I need to populate
I always thought an SQL compiler would break but apparently nesing can nearly be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.