Jump to content

Wildcard SQL


arunpatal
Go to solution Solved by DavidAM,

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
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
Edited by arunpatal
Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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