Jump to content

[SOLVED] substr($num) Advice


phpnewbie112

Recommended Posts

Hello,

 

I really need your advices and ideas about the given problem.

 

I have a field in a form dedicated to enter international format mobile numbers example 1928123456, 336123456789, 4156897526, 2010102030

 

I have a mysql tables with 3 fields:

Country - Code - Rate

 

example:

France - 336 - 5

USA - 1 - 4

 

How can I make the following code work but while reading the values from the db rather than a manual one:

 

if(substr($num,0,3)=="336") {

    $rate = "5";

}

 

else

if(substr($num,0,1)=="1") {

    $rate = "4";

}

 

 

 

Link to comment
Share on other sites

try this:

 

<?php
  $number = '1928123456';
  $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',0,LENGTH(code)) = code",mysql_real_escape_string($number));
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    print_r($row);
  }
?>

Link to comment
Share on other sites

oops...1 not 0:

 

<?php
  $number = '1928123456';
  $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number));
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    print_r($row);
  }
?>

Link to comment
Share on other sites

<?php
  $number = '1928123456';
  $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number));
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    print $row['rate'];
  }
?>

Link to comment
Share on other sites

<?php
  $number = '1928123456';
  $sql = sprintf("SELECT * FROM rates WHERE SUBSTRING('%s',1,LENGTH(code)) = code",mysql_real_escape_string($number));
  $result = mysql_query($sql) or die(mysql_error());
  if(mysql_num_rows($result)){
    while($row = mysql_fetch_assoc($result)){
      print $row['rate'];
    }
  }else{
    print "Not included";
  }
?>

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.