-1

In my mysql database I have table 'class' with column 'classname' contain : Andree,adele,Beta,Alfa

How can I get all the names in 'classname' whose first letter is 'a' or 'A' by php code ? I tried many ways to do this but they did not give the correct result

alestanis
  • 21,519
  • 4
  • 48
  • 67
Phuc Khung
  • 29
  • 1
  • 6
  • 3
    RTLM? http://php.net/substr But don't do filtering in PHP. you don't buy up an entire store's inventory, take it home, then throw everything away just because you only wanted 1 candy bar. – Marc B Mar 08 '13 at 14:05

3 Answers3

3

SELECT classname FROM class WHERE classname LIKE 'a%'

You'll use a query similar to this somewhere in your PHP code... We'd need a sample of your PHP to give you anything more than that.

brbcoding
  • 13,378
  • 2
  • 37
  • 51
1

See substr on the php docs.

$firstLetter = substr($string, 0, 1);
soyuka
  • 8,839
  • 3
  • 39
  • 54
0
$result = mysql_query("SELECT classname FROM `class` WHERE classname LIKE 'a%'"); 

note that LIKE is case-insensitive.

Lonxx
  • 1
  • 1
  • 2
  • Just an FYI, `mysql_query()` should no longer be used... Documentation states "Instead, the MySQLi or PDO_MySQL extension should be used." – brbcoding Mar 08 '13 at 14:06