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 8897663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:24:16+00:00 2026-06-15T00:24:16+00:00

I run a query that returns the first word only from a particular field,

  • 0

I run a query that returns the first word only from a particular field, this field has multiple words separated by spaces. The result of the query will be value of the select box I create dynamically. I have 3 select boxes that are created dynamically depending on the selection of the other.

Problem is query returns entire sentence and that is displayed in select box as well. I tried running the query solely in database and it seems to give me the correct result.

Here is the code snippet. How can i resolve it??

<?php
  //**************************************
 //     First selection results     //
//**************************************
if(isset($_GET['func'])&& $_GET['func'] == "drop_1") {
   drop_1($_GET['drop_var']); 
}

function drop_1($drop_var)
{
$result = mysql_query("SELECT DISTINCT SUBSTRING_INDEX(`h_name` ,' ', 1 ) AS name FROM hypermarket_em") or die(mysql_error());
echo '<select name="drop_2" id="drop_2"><option value=" " disabled="disabled" selected="selected">Select City</option>
        <option value="ALL" >ALL</option>';
while($drop_2 = mysql_fetch_array( $result )) 
{
      echo '<option value="'.$drop_2['name'].'">'.$drop_2['name'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
    $('#wait_2').hide();
    $('#drop_2').change(function(){
    $(this).parent().parent().find('.drop2').val($(this).val());
    $('#wait_2').show();
    $('#result_2').hide();
    $.get(\"func.php\", {
    func: \"drop_2\",
    drop_var: $('#drop_2').val()
    }, function(response){
    $('#result_2').fadeOut();
    setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
    });
    return false;
    });
    </script>";
}
?>

Any ideas much appreciated. Thanks.

  • 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-06-15T00:24:18+00:00Added an answer on June 15, 2026 at 12:24 am

    You are setting the substring

    SUBSTRING_INDEX(`h_name` ,' ', 1 ) AS name
    

    but still referencing the full original string $drop_2['h_name']. Try changing to –

    while($drop_2 = mysql_fetch_array( $result )) 
    {
      echo '<option value="'.$drop_2['name'].'">'.$drop_2['name'].'</option>';
    }
    

    EDIT–

    If you are correctly using the alias name in $drop_2['name'], I am not sure why it is still echoing the full string. You could use the explode() that I first answered with, as a fail safe

    while($drop_2 = mysql_fetch_array( $result )) 
    {
      $firstWord = explode(' ',trim($drop_2['name']));
      echo '<option value="'.$firstWord[0].'">'.$firstWord[0].'</option>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a query that returns the first word only from
In PostgreSQL I run a query on it with several conditions that returns multiple
I need to run a query that returns a list of users, along with
I have a custom SQL query that I run with this line: @avg_score =
I have a java.sql.ResultSet object containg data from a query that was run. How
This is my first crack at a method that is run periodically during the
I have the following Table elements how do i run a query that reproduce
I am using Oracle 11.1.0.6.0 and ADO.NET (System.Data.OracleClient) to run a query that works
I would like to run a (extensive) query that produces one line of XML.
I have the next query that I run via SQLCMD.EXE use [AxDWH_Central_Reporting] GO EXEC

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.