phpnewbie112 Posted October 23, 2008 Share Posted October 23, 2008 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"; } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 23, 2008 Share Posted October 23, 2008 Pardon... You're working out the 'rate' within PHP when you already have the 'rate' stored in the database. I dont get that Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 wildteen88 I am showing this as an example. I want to get both code and rates from database based on the number I have, the phpcode shall give based on the number the appropriate code and corresponding rate. rhodesa I will test your code thanks Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 rhodesa it is returning an empty page Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 interesting; we have some hints now; it is showing: Array ( [id] => 1 [country] => USA [c0de] => 1 [rate] => 4 ) how can we extract only the rate value from this array? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 <?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']; } ?> Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 23, 2008 Author Share Posted October 23, 2008 wow thanks a million you're a genius! Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 24, 2008 Author Share Posted October 24, 2008 One more question if you don't mind, I tried to include the following: if isempty($row['rate']) { echo "Not Included"; } so when there is no return rate, it gives "not included" message but it is return nothing. pls advice. thanks a million Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 24, 2008 Share Posted October 24, 2008 <?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"; } ?> Quote Link to comment Share on other sites More sharing options...
phpnewbie112 Posted October 24, 2008 Author Share Posted October 24, 2008 Thanks I really appreciate your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.