0

Am trying to display a string from my database where the first character is between 0 to 9 i tried it but still did not work below is my code help me with it thanks.

if(isset($_REQUEST['num'])){ $num = explode('-',$_REQUEST['num']);
$query1 = "SELECT usercode FROM user WHERE code like '".$num."%'" ORDER BY id DESC ";
    $result1 = mysql_query ($query1) or die('query error');
    while( $line1 = mysql_fetch_assoc($result1)){
    echo $line1[usercode];
    }

<a href="home.php?num=<?php echo urlencode('0-9');?>">#</a>
simon oni
  • 35
  • 6

2 Answers2

0

Your code is currently searching your database for Array since you are explode()'ing the $_REQUEST['num'], which returns an array.

You may want to create a range of numbers from your $num and then do multiple LIKE's or maybe try with a REGEXP.

cOle2
  • 4,725
  • 1
  • 24
  • 26
0

Use the REGEXP function in MySQL to match code to ^[0-9] which means: "the first letter must be between 0 and 9" and in ASCII, they happen to be the numbers between 0 and 9.

This will do what you want:

<?php
if (isset($_REQUEST['num'])) {
    $num = explode('-', $_REQUEST['num']);
    $query1 = "SELECT usercode FROM user WHERE code REGEXP '^[" . $num[0] . "-" . $num[1] . "]' ORDER BY id DESC";
    $result1 = mysql_query ($query1) or die('query error');
    while ($line1 = mysql_fetch_assoc($result1)) {
        echo $line1['usercode'];
    }
}
?>

<a href="home.php?num=<?php echo urlencode('0-9'); ?>">#</a>

cOle2 is also right about the explode function. It returns an array, the elements of which are the input string tokenized by some delimiter. In your case, $_REQUEST['num'] based on a delimiter -.

Steven Linn
  • 696
  • 5
  • 14