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

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
This assumes you have already connected to the database to run the query.PHP Code:<?
$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]";
}
}
?>
Last edited by Florx; 11-11-2007 at 10:11 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
This assumes you have already connected to the database to run the query.PHP Code:<?
$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]";
}
}
?>
Alrighty
Good luck with the PHP.
You can just do this with SQL...
PHP Code:<?
$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..
@Blob:
http://dev.mysql.com/doc/refman/5.0/...-matching.html
Read the first bit on the page.
Want to hide these adverts? Register an account for free!