Advertisement
dropbox1349

mysql encoding check

Mar 3rd, 2015
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?
  2. error_reporting(0);
  3. function runSQL($rsql) {
  4.     $hostname = "localhost";
  5.     $username = "root";
  6.     $password = "";
  7.     $dbname   = "country";
  8.     $connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
  9.     $db = mysql_select_db($dbname);
  10.     $result = mysql_query($rsql) or die ('test');
  11.     return $result;
  12.     mysql_close($connect);
  13. }
  14.  
  15. function countRec($fname,$tname,$where) {
  16. $sql = "SELECT count($fname) FROM $tname $where";
  17. $result = runSQL($sql);
  18. while ($row = mysql_fetch_array($result)) {
  19. return $row[0];
  20. }
  21. }
  22. $page = $_POST['page'];
  23. $rp = $_POST['rp'];
  24. $sortname = $_POST['sortname'];
  25. $sortorder = $_POST['sortorder'];
  26.  
  27. if (!$sortname) $sortname = 'name';
  28. if (!$sortorder) $sortorder = 'desc';
  29.         if($_POST['query']!=''){
  30.             $where = "WHERE `".$_POST['qtype']."` LIKE '%".$_POST['query']."%' ";
  31.         } else {
  32.             $where ='';
  33.         }
  34.         if($_POST['letter_pressed']!=''){
  35.             $where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' ";  
  36.         }
  37.         if($_POST['letter_pressed']=='#'){
  38.             $where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' ";
  39.         }
  40. $sort = "ORDER BY $sortname $sortorder";
  41.  
  42. if (!$page) $page = 1;
  43. if (!$rp) $rp = 10;
  44.  
  45. $start = (($page-1) * $rp);
  46.  
  47. $limit = "LIMIT $start, $rp";
  48.  
  49. $sql = "SELECT id,iso,name,printable_name,iso3,numcode FROM country $where $sort $limit";
  50. $result = runSQL($sql);
  51.  
  52. $total = countRec('iso','country',$where);
  53.  
  54. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  55. header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  56. header("Cache-Control: no-cache, must-revalidate" );
  57. header("Pragma: no-cache" );
  58. header("Content-type: text/x-json");
  59. $json = "";
  60. $json .= "{\n";
  61. $json .= "page: $page,\n";
  62. $json .= "total: $total,\n";
  63. $json .= "rows: [";
  64. $rc = false;
  65. while ($row = mysql_fetch_array($result)) {
  66. if ($rc) $json .= ",";
  67. $json .= "\n{";
  68. $json .= "id:'".$row['id']."',";
  69. $json .= "cell:['".$row['id']."','".$row['iso']."'";
  70. $json .= ",'".addslashes($row['name'])."'";
  71. $json .= ",'".addslashes($row['printable_name'])."'";
  72. $json .= ",'".addslashes($row['iso3'])."'";
  73. $json .= ",'".addslashes($row['numcode'])."']";
  74. $json .= "}";
  75. $rc = true;
  76. }
  77. $json .= "]\n";
  78. $json .= "}";
  79. echo $json;
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement