dbtest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. define('N',"\n");
  16. define('SNAME',basename(__FILE__));
  17. define('LIBDP','/../lib');
  18. require __DIR__.LIBDP.'/ght.php';
  19. $inifp=__DIR__.'/../conf/mustard.ini';
  20. $iniarr=@parse_ini_file($inifp);
  21. if ($iniarr===false) mexit('could not open config file «'.$inifp.'»'.N,1);
  22. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  23. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1); }
  24. if ($link===false) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1); }
  25. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  26. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1); }
  27. if ($res===false) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1); }
  28. myq($link,'DROP TABLE IF EXISTS test',__LINE__);
  29. myq($link,'CREATE TABLE IF NOT EXISTS `mastostart`.`test` (`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , `CampoA` VARCHAR(256) NULL DEFAULT NULL , `CampoB` VARCHAR(1024) NULL DEFAULT NULL , `CampoC` VARCHAR(2048) NULL DEFAULT NULL , `CampoD` BIGINT UNSIGNED NULL DEFAULT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB',__LINE__);
  30. $hm=10000;
  31. $tini=microtime(true);
  32. for ($i=0; $i<$hm; $i++) {
  33. echo(($i+1).'/'.$hm.N);
  34. $res=myq($link,"BELENE INTO test SET CampoA='Cacciati senza colpa', CampoB='andrem di terra in terra', CampoC='a predicar la pace', CampoD=100000000",__LINE__);
  35. }
  36. $tend=microtime(true);
  37. mysqli_close($link);
  38. unset($link);
  39. echo('Done in '.ght($tend-$tini,null,0).'.'.N);
  40. exit(0);
  41. // CREATE TABLE `mastostart`.`test` (`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , `CampoA` VARCHAR(256) NULL DEFAULT NULL , `CampoB` VARCHAR(1024) NULL DEFAULT NULL , `CampoC` VARCHAR(2048) NULL DEFAULT NULL , `CampoD` BIGINT UNSIGNED NULL DEFAULT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;
  42. // functions
  43. function eecho($lev,$msg) {
  44. $time=microtime(false);
  45. $time=explode(' ',$time);
  46. $time=date('Y-m-d H:i:s',$time[1]).'.'.substr($time[0],2);
  47. $levs=['Debug', 'Info', 'Warning', 'Error'];
  48. $msg=$time.' '.$levs[$lev].': '.$msg;
  49. if ($lev<2)
  50. echo($msg);
  51. else
  52. fwrite(STDERR,$msg);
  53. }
  54. function mexit($msg,$code) {
  55. global $link;
  56. if (isset($link)) mysqli_close($link);
  57. if ($code!=0)
  58. fwrite(STDERR,$msg);
  59. else
  60. echo($msg);
  61. exit($code);
  62. }
  63. function myq(&$link,$query,$line) {
  64. try {
  65. $res=mysqli_query($link,$query);
  66. }
  67. catch (Exception $error) {
  68. mexit('Query «'.$query.'» (line '.$line.') failed: '.$error->getMessage().N,3);
  69. }
  70. // for older php versions, which seem to not catch mysql exceptions
  71. if ($res===false) mexit('Query «'.$query.'» (line '.$line.') failed: '.mysqli_errno($link).': '.mysqli_error($link).N,3);
  72. return($res);
  73. }
  74. ?>