mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 I am trying to get just numeric values from database table. i use ctype_digit and is_numeric. yeh i know it works fine. but when i use while loop ,in the else statement it shows either empty OR any value if i assign to the variable. Code: <?php $sql="select * from user where mobile !=''"; $query = $db->sql_query($sql) or die (mysql_error()); while($row = $db->sql_fetchrow($query)) { if(ctype_digit($row['mobile']))//can also use is_numeric function { $mobile=($row['mobile']); }else { $mobile = 'its not digit'; } echo $mobile; ?> i dont want to show the empty OR 'its not digit' sentence. any ideas. hope i clarify it well. Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/ Share on other sites More sharing options...
MasterACE14 Posted July 1, 2008 Share Posted July 1, 2008 try this: <?php $sql = "select * from user where mobile !=''"; $query = $db->sql_query($sql) or die(mysql_error()); $row = $db->sql_fetchrow($query); if (ctype_digit($row['mobile'])) //can also use is_numeric function { $mobile = $row['mobile']; } else { $mobile = 'its not digit'; } echo $mobile; ?> ACE Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578853 Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks. may i know what changes u did except this: $mobile = $row['mobile']; Because the result is still the same. Edit: oh,u remove while loop.but i need all mobile numbers.not just one. Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578862 Share on other sites More sharing options...
MasterACE14 Posted July 1, 2008 Share Posted July 1, 2008 I'm not exactly sure what this does... $db->sql_fetchrow($query); but using mysql, you can use: $row = mysql_fetch_array($query); without a loop and it grabs every result. Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578867 Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Author Share Posted July 1, 2008 latest code: <?php $sql="select * from user where mobile !=''"; $query = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_array($query); if(ctype_digit($row['mobile']))//can also use is_numeric function { $mobile=$row['mobile']; }else { $mobile = 'its not digit'; } echo $mobile; ?> giving me just one result. Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578874 Share on other sites More sharing options...
MasterACE14 Posted July 1, 2008 Share Posted July 1, 2008 is there only 1 entry in the table that is a valid phone number? Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578882 Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Author Share Posted July 1, 2008 Ok,to be more clear. i have these entries in table. 0123824061 its not digit its not digit 016 0165241 016 the following code retrieve these entries: <?php $sql="select * from user where mobile !=''"; $query = $db->sql_query($sql) or die (mysql_error()); while($row = $db->sql_fetchrow($query)) { if(ctype_digit($row['mobile']))//can also use is_numeric function { $mobile=$row['mobile']; }else { $mobile = 'its not digit'; } echo $mobile; echo "<br>"; } ?> Now i want these values: 0123824061 016 0165241 016 but if remove else statement OR make that variable empty in else statement like: else{ $mobile =''; } Then the result is: 0123824061 016 0165241 016 mean showing two empty rows for the alpha. which i dont want. hope i clear it well. Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578887 Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Author Share Posted July 1, 2008 i solved it: <?php $sql="select * from user where mobile !=''"; $query = $db->sql_query($sql) or die (mysql_error()); while($row = $db->sql_fetchrow($query)) { $mobile=''; if(ctype_digit($row['mobile']))//can also use is_numeric function { $mobile=($row['mobile']); } if (!empty($mobile)) { echo $mobile; } } ?> Link to comment https://forums.phpfreaks.com/topic/112719-solved-sort-out-numeric-values/#findComment-578902 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.