View Full Version : [PHP/SQL] First letter of name
[Oli]
11-11-2007, 11:00 AM
Okay so I have a table with my users in it.
I have a page that displays all my users sorted alphabetical.
Now if I wanted to show only the users that have a name that starts with the letter "A or a" how do I do this in php/Sql ?
+rep
Thanks,
Olivier
Florx
11-11-2007, 10:04 PM
<?
$sql = mysql_query("SELECT * FROM users ORDER BY username ASC");
while ($r = mysql_fetch_array($sql)){
$str = $r[name];
$first = $str[0];
if($first == "A" || $first == "a"){
echo "$r[name]";
}
}
?>
This assumes you have already connected to the database to run the query.
[Oli]
11-11-2007, 10:20 PM
Trough googling I had already found a solution, but i'll fav. ur post so i dont forget for other projects.
+rep for helping ;)
ty,
oli
<?
$sql = mysql_query("SELECT * FROM users ORDER BY username ASC");
while ($r = mysql_fetch_array($sql)){
$str = $r[name];
$first = $str[0];
if($first == "A" || $first == "a"){
echo "$r[name]";
}
}
?>
This assumes you have already connected to the database to run the query.
Florx
12-11-2007, 05:05 PM
Alrighty :)
Good luck with the PHP.
You can just do this with SQL...
<?
$sql = mysql_query("SELECT * FROM users WHERE username LIKE 'a%' OR LIKE 'A%' ORDER BY username ASC");
while ($r = mysql_fetch_array($sql)){
echo $r['username'] . '<br />';
}
?>
You can just do this with SQL...
<?
$sql = mysql_query("SELECT * FROM users WHERE username LIKE 'a%' OR LIKE 'A%' ORDER BY username ASC");
while ($r = mysql_fetch_array($sql)){
echo $r['username'] . '<br />';
}
?>
No that will make any name with the letter A in it will show..
RYANNNNN
13-11-2007, 06:12 PM
@Blob:
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html
Read the first bit on the page.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.