lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

php_mysql:  PHP 格式化输出数据

 

 

 

 

一、php 源程序

 1 [wit@fedora mysql]$ cat  php_mysql 
 2 #!/usr/bin/php
 3 
 4 
 5 <?php
 6 
 7 
 8 
 9 
10 // filename = php_mysql
11 
12 
13 
14 
15 function test()
16 {
17         echo "hello, php!\n";
18 }
19 
20 
21 function php_mysql()
22 {
23 
24         $server = "localhost";
25         $user = "user";
26         $passwd = "mima";
27         $dbname = "tmp";
28 
29 
30         try
31         {
32                 $dbh = new PDO("mysql:host=$server;dbname=$dbname", $user, $passwd);
33                 $dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34 
35                 $icount = 0;
36                 echo "\n\n";
37                 foreach ( $dbh->query('select * from dlt2023') as $row)
38                 {
39                         // echo "\n\t----  [tmp:$icount]  ----\t\n";
40                         // print_r($row);
41 
42                         // zhong dian
43                         // gongji 4 ge kong ge
44                         // row[i] != 0, row[i] = "1 kongge" + row[i];    row[i] == NULL, row[i] = "2 kongge".
45                         for($i=0; $i<49; $i++)
46                         {
47                                 if ( ($row[$i] != 0) and ($row[$i] < 10) )
48                                 {
49                                         // 1 ge kongge
50                                         $row[$i] = " "  . $row[$i];
51                                 }
52                                 else if ( $row[$i] == NULL )
53                                 {
54                                         // 2 ge kongge
55                                         $row[$i] = "  ";
56                                 }
57                         }
58 
59                         echo "$row[0]  $row[1]  $row[2]  $row[3]  $row[4]  $row[5]  $row[6]  $row[7]  $row[8]  $row[9]";
60                         echo "  $row[10]  $row[11]  $row[12]  $row[13]  $row[14]  $row[15]  $row[16]  $row[17]  $row[18]  $row[19]";
61                         echo "  $row[20]  $row[21]  $row[22]  $row[23]  $row[24]  $row[25]  $row[26]  $row[27]  $row[28]  $row[29]";
62                         echo "  $row[30]  $row[31]  $row[32]  $row[33]  $row[34]  $row[35]  $row[36]  --  $row[37]  $row[38]  $row[39]";
63                         echo "  $row[40]  $row[41]  $row[42]  $row[43]  $row[44]  $row[45]  $row[46]  $row[47]  $row[48]";
64                         $icount = $icount + 1;
65                         echo "\n";
66                         echo "\n";
67                 }
68                 echo "\n\n";
69 
70                 $icount = 0;
71                 $dbh = null;
72 
73         }
74         catch(PDOException $e)
75         {
76                 die( "Error!: " . $e->getMessage() . "<br/>");
77         }
78 
79 }
80 
81 
82 
83 
84 
85 
86 // test php_mysql
87 php_mysql();
88 
89 
90 ?>
91 [wit@fedora mysql]$ 
92 [wit@fedora mysql]$

 

 

 

 

 二、输出结果

 1 [wit@fedora mysql]$   ./php_mysql 
 2 
 3 
 4 
 5 
 6 2023000  2023-01-01   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  --   1   2   3   4   5   6   7   8   9  10  11  12
 7 
 8 2023001  2023-01-02                                                                      18  19                  24          27                          34      --                                          11  12
 9 
10 2023002  2023-01-04                                          11          14                          21                  26  27                                  --       2                   7                    
11 
12 2023003  2023-01-07                   5   6                              14                          21  22                                                      --       2                           9            
13 
14 
15 
16 [wit@fedora mysql]$ 
17 [wit@fedora mysql]$ 

 

 

 

 

三、参考资料

 

  1、PHP PDO| 菜鸟教程    https://www.runoob.com/php/php-pdo.html

 

posted on 2023-11-18 08:19  lnlidawei  阅读(21)  评论(0编辑  收藏  举报