Jump to content

MySQL DISTINCT & TRIM problem


dennismonsewicz

Recommended Posts

There's nothing technically wrong with that query. It would taking seeing your code from the point where you form the query though to the code that is determining that there is no results in order to determine what is wrong. Also, post a sample of your data that you expect the query to return.

Link to comment
Share on other sites

here is my query:

$man_db = mysql_query("SELECT DISTINCT TRIM(manufacturer) FROM portal_deductible WHERE carrier = 'carrier2'")or die(mysql_error());

 

My results are being populated like so:

 

<select name="manufacturer" onchange="this.form.submit();">
	<option>-- Select Manufacturer --</option>
	<?php
		while($row = mysql_fetch_object($man_db)) {
			if($_POST['manufacturer'] == $row->manufacturer) {
				$s = " SELECTED";
			} else {
				$s = "";
			}
			echo '<option value="' . $row->manufacturer . '" ' . $s . '>' . $row->manufacturer . '</option>';
		}
	?>
</select>

 

I don't get a mysql error or anything, just no results are displayed

Link to comment
Share on other sites

If you do a "view source" of the page in your browser, you will likely see a php error that $row->manufacturer does not exist.

 

The expression you put into the SELECT term in a query is exactly what you get in the result set. You would actually get something like $row->TRIM(manufacturer), but since that is not valid object notation, you would not be able to access the value unless you use an alias name in the query.

 

Use the following -

 

"SELECT DISTINCT TRIM(manufacturer) as manufacturer FROM portal_deductible WHERE carrier = 'carrier2'"

 

You should also check how many rows there are in the result set and take an appropriate action, like displaying a user message "There are no matching entries" instead of displaying an empty select menu.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.