Avatar of array007
array007 asked on

Using mysql substring result in php mysql_fetch_array

Looks like I got myself in a new problem
I know "AS" is redundant I just added it as a trial.

I am not able to use the result in a mysql_fetch_array
Note: the query runs fine and returns expected results

$result = $db->query("
SELECT DISTINCT
SUBSTRING (username,1,1)
AS username
FROM 
my_table
WHERE
username LIKE '%'
");
 
echo '<select name="username">';
		
while ($row=$db->fetch_array($result) )
{
	$username=$row['username'];
	echo '<option>';
	echo"$username";
	echo"</options>";
}

Open in new window

PHP

Avatar of undefined
Last Comment
Roger Baklund

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

please try this:
$result = $db->query("
SELECT DISTINCT SUBSTRING (t.username,1,1) AS username
FROM my_table t
WHERE t.username LIKE '%'
");
 

Open in new window

ASKER
array007

Thanks, but I still get only blank in the DD box
Guy Hengel [angelIII / a3]

sorry, the space before the "(" is not good:
$result = $db->query("
SELECT DISTINCT SUBSTRING(t.username,1,1) AS username
FROM my_table t
WHERE t.username LIKE '%'
");

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
array007

I made the correction before testing
so it stillis getting empty DD box
ASKER
array007

but aren't you posting the same query twice I do not see nay change, or is it me?
Roger Baklund

I think the problem is in line 18 of your snippet:

echo"</options>";

Change this to:

echo"</option>";
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
array007

You did it that space was killing it
thank you
Roger Baklund

Really...? </options> was not the problem?
ASKER
array007

as the end of a select session
it did not generate any error on  my local machine lamp Apache 2 PHP 5.2+ MySQlL5
altough while I had that typo I did not try passing any values
my focus was on the "blank" DD box value.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Roger Baklund

It should not produce any error message, it is just invalid html... I first expected it would result in a blank dropdown, but it makes sense that it does not, as <option> has an optional end tag... it is legal (in html, not in xhtml) to write <select><option>A<option>B<option>C</select>.