Jump to content

Wildcard SQL


arunpatal

Recommended Posts

Hi, i am trying to use charlist wildcard but its not working.....

 

Please check and help

 

I want to display all username starting with S,P and N.

<?php

require("connect/connection.php");

if (!empty ($_POST['start'])) {
$start = $_POST['start'];
$result = mysql_query("SELECT * FROM $demo WHERE username LIKE '[$start]%'"); 
while ($row = mysql_fetch_array($result)) {
echo "$row[username] from $row[country]<br/>";
}} 
?>


<form method='post'>
<input type='text' name='start' placeholder="ugn" size='50'/><br><br>
<input type='submit' value='Search' />
</form>
Link to comment
https://forums.phpfreaks.com/topic/283470-wildcard-sql/
Share on other sites

I'm guessing that you're after S, P or N... not and!

 

Try using RLIKE instead: http://dev.mysql.com/doc/refman/5.0/en/regexp.html

 

Otherwise you'll need to explode the string and recreate a statement with OR's between... all within the LIKE

 

True....

 

I want to display all username starting with S, P and N...

 

I am following this tutorial but still not working.......

 

http://www.w3resource.com/sql/wildcards-like-operator/wildcards-charlist.php

Tutorial
Link to comment
https://forums.phpfreaks.com/topic/283470-wildcard-sql/#findComment-1456360
Share on other sites

I'm sure there's a better way, but you could try something like this:

<?php
$start = str_split($_POST['start']);
foreach($start as $currKey=>$currLetter) {
    $start[$currKey] = "username LIKE '" . $currLetter . "%'";
}
$result = mysql_query("SELECT * FROM $demo WHERE " . implode(' OR ', $start));
?>
Link to comment
https://forums.phpfreaks.com/topic/283470-wildcard-sql/#findComment-1456375
Share on other sites

So am using it like  this now.......

but the problem is that it shows all username which have S P N in it.

<?php
require("connect/connection.php");

$result = mysql_query("SELECT * FROM $demo WHERE username RLIKE '[SPN]'");
while ($row = mysql_fetch_array($result)) {
echo "$row[username] from $row[country]<br/>";
}
?>

I want only those username which are starting with letter S P and N

Link to comment
https://forums.phpfreaks.com/topic/283470-wildcard-sql/#findComment-1456377
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.