Home >Backend Development >PHP Tutorial >php module reference, php_PHP tutorial
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>//</span><span>数据库连接类</span> <span> 3</span> <span>class</span><span> ConnDB{ </span><span> 4</span> <span> 5</span> <span>var</span> <span>$dbtype</span><span>; </span><span> 6</span> <span>var</span> <span>$host</span><span>; </span><span> 7</span> <span>var</span> <span>$user</span><span>; </span><span> 8</span> <span>var</span> <span>$pwd</span><span>; </span><span> 9</span> <span>var</span> <span>$dbname</span><span>; </span><span> 10</span> <span> 11</span> <span>//</span><span>构造方法</span> <span> 12</span> <span>function</span> ConnDB(<span>$dbtype</span>,<span>$host</span>,<span>$user</span>,<span>$pwd</span>,<span>$dbname</span><span>){ </span><span> 13</span> <span>$this</span>->dbtype=<span>$dbtype</span><span>; </span><span> 14</span> <span>$this</span>->host=<span>$host</span><span>; </span><span> 15</span> <span>$this</span>->user=<span>$user</span><span>; </span><span> 16</span> <span>$this</span>->pwd=<span>$pwd</span><span>; </span><span> 17</span> <span>$this</span>->dbname=<span>$dbname</span><span>; </span><span> 18</span> <span> } </span><span> 19</span> <span> 20</span> <span>//</span><span>实现数据库的连接并返回连接对象</span> <span> 21</span> <span>function</span><span> GetConnId(){ </span><span> 22</span> <span> 23</span> <span>if</span>(<span>$this</span>->dbtype=="mysql" || <span>$this</span>->dbtype=="mssql"<span>){ </span><span> 24</span> <span>$dsn</span>="<span>$this</span>->dbtype:host=<span>$this</span>->host;dbname=<span>$this</span>->dbname"<span>; </span><span> 25</span> }<span>else</span><span>{ </span><span> 26</span> <span>$dsn</span>="<span>$this</span>->dbtype:dbname=<span>$this</span>->dbname"<span>; </span><span> 27</span> <span> } </span><span> 28</span> <span>try</span><span> { </span><span> 29</span> <span>$conn</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$this</span>->user, <span>$this</span>->pwd); <span>//</span><span>初始化一个PDO对象,就是创建了数据库连接对象$pdo</span> <span> 30</span> <span>$conn</span>->query("set names utf8"<span>); </span><span> 31</span> <span>return</span> <span>$conn</span><span>; </span><span> 32</span> } <span>catch</span> (PDOException <span>$e</span><span>) { </span><span> 33</span> <span>die</span> ("Error!: " . <span>$e</span>->getMessage() . "<br/>"<span>); </span><span> 34</span> <span> } </span><span> 35</span> <span> 36</span> <span> 37</span> <span> } </span><span> 38</span> <span>} </span><span> 39</span> <span> 40</span> <span>//</span><span>数据库管理类</span> <span> 41</span> <span>class</span><span> AdminDB{ </span><span> 42</span> <span> 43</span> <span> 44</span> <span>function</span> ExecSQL(<span>$sqlstr</span>,<span>$conn</span><span>){ </span><span> 45</span> <span> 46</span> <span>$sqltype</span>=<span>strtolower</span>(<span>substr</span>(<span>trim</span>(<span>$sqlstr</span>),0,6<span>)); </span><span> 47</span> <span>$rs</span>=<span>$conn</span>->prepare(<span>$sqlstr</span>); <span>//</span><span>准备查询语句</span> <span> 48</span> <span>$rs</span>->execute(); <span>//</span><span>执行查询语句,并返回结果集</span> <span> 49</span> <span>if</span>(<span>$sqltype</span>=="select"<span>){ </span><span> 50</span> <span>$array</span>=<span>$rs</span>->fetchAll(PDO::FETCH_ASSOC); <span>//</span><span>获取结果集中的所有数据</span> <span> 51</span> <span>if</span>(<span>count</span>(<span>$array</span>)==0 || <span>$rs</span>==<span>false</span><span>) </span><span> 52</span> <span>return</span> <span>false</span><span>; </span><span> 53</span> <span>else</span> <span> 54</span> <span>return</span> <span>$array</span><span>; </span><span> 55</span> }<span>elseif</span> (<span>$sqltype</span>=="update" || <span>$sqltype</span>=="insert" || <span>$sqltype</span>=="delete"<span>){ </span><span> 56</span> <span>if</span>(<span>$rs</span><span>) </span><span> 57</span> <span>return</span> <span>true</span><span>; </span><span> 58</span> <span>else</span> <span> 59</span> <span>return</span> <span>false</span><span>; </span><span> 60</span> <span> } </span><span> 61</span> <span> } </span><span> 62</span> <span>} </span><span> 63</span> <span>//</span><span>分页类</span> <span> 64</span> <span>class</span><span> SepPage{ </span><span> 65</span> <span>var</span> <span>$rs</span><span>; </span><span> 66</span> <span>var</span> <span>$pagesize</span><span>; </span><span> 67</span> <span>var</span> <span>$nowpage</span><span>; </span><span> 68</span> <span>var</span> <span>$array</span><span>; </span><span> 69</span> <span>var</span> <span>$conn</span><span>; </span><span> 70</span> <span>var</span> <span>$sqlstr</span><span>; </span><span> 71</span> <span>function</span> ShowData(<span>$sqlstr</span>,<span>$conn</span>,<span>$pagesize</span>,<span>$nowpage</span>){ <span>//</span><span>定义方法</span> <span> 72</span> <span>if</span>(!<span>isset</span>(<span>$nowpage</span>) || <span>$nowpage</span>=="") <span>//</span><span>判断变量值是否为空</span> <span> 73</span> <span>$this</span>->nowpage=1; <span>//</span><span>定义每页起始页</span> <span> 74</span> <span>else</span> <span> 75</span> <span>$this</span>->nowpage=<span>$nowpage</span><span>; </span><span> 76</span> <span>$this</span>->pagesize=<span>$pagesize</span>; <span>//</span><span>定义每页输出的记录数</span> <span> 77</span> <span>$this</span>->conn=<span>$conn</span>; <span>//</span><span>连接数据库返回的标识</span> <span> 78</span> <span>$this</span>->sqlstr=<span>$sqlstr</span>; <span>//</span><span>执行的查询语句</span> <span> 79</span> <span>$offset</span>=(<span>$this</span>->nowpage-1)*<span>$this</span>-><span>pagesize; </span><span> 80</span> <span>$sql</span>=<span>$this</span>->sqlstr." limit <span>$offset</span>, <span>$this</span>->pagesize"<span>; </span><span> 81</span> <span>$result</span>=<span>$this</span>->conn->prepare(<span>$sql</span>); <span>//</span><span>准备查询语句</span> <span> 82</span> <span>$result</span>->execute(); <span>//</span><span>执行查询语句,并返回结果集</span> <span> 83</span> <span>$this</span>-><span>array</span>=<span>$result</span>->fetchAll(PDO::FETCH_ASSOC); <span>//</span><span>获取结果集中的所有数据</span> <span> 84</span> <span>if</span>(<span>count</span>(<span>$this</span>-><span>array</span>)==0 || <span>$this</span>-><span>array</span>==<span>false</span><span>) </span><span> 85</span> <span>return</span> <span>false</span><span>; </span><span> 86</span> <span>else</span> <span> 87</span> <span>return</span> <span>$this</span>-><span>array</span><span>; </span><span> 88</span> <span> } </span><span> 89</span> <span> 90</span> <span>function</span> ShowPage(<span>$contentname</span>,<span>$utits</span>,<span>$anothersearchstr</span>,<span>$anothersearchstrs</span>,<span>$class</span><span>){ </span><span> 91</span> <span>$str</span>=""<span>; </span><span> 92</span> <span>$res</span>=<span>$this</span>->conn->prepare(<span>$this</span>->sqlstr); <span>//</span><span>准备查询语句</span> <span> 93</span> <span>$res</span>->execute(); <span>//</span><span>执行查询语句,并返回结果集</span> <span> 94</span> <span>$this</span>-><span>array</span>=<span>$res</span>->fetchAll(PDO::FETCH_ASSOC); <span>//</span><span>获取结果集中的所有数据 </span> <span> 95</span> <span>$record</span>=<span>count</span>(<span>$this</span>-><span>array</span>); <span>//</span><span>统计记录总数</span> <span> 96</span> <span> 97</span> <span>$pagecount</span>=<span>ceil</span>(<span>$record</span>/<span>$this</span>->pagesize); <span>//</span><span>计算共有几页</span> <span> 98</span> <span>$str</span>.=<span>$contentname</span>." ".<span>$record</span>." ".<span>$utits</span>." 每页 ".<span>$this</span>->pagesize." ".<span>$utits</span>." 第 ".<span>$this</span>->nowpage." 页/共 ".<span>$pagecount</span>." 页"<span>; </span><span> 99</span> <span>$str</span>.=" "<span>; </span><span>100</span> <span>if</span>(<span>$this</span>->nowpage!=1<span>) </span><span>101</span> <span>$str</span>.="<a href=".<span>$_SERVER</span>['PHP_SELF']."?page=1&page_type=".<span>$anothersearchstr</span>."¶meter2=".<span>$anothersearchstrs</span>." class=".<span>$class</span>.">首页</a>"<span>; </span><span>102</span> <span>else</span> <span>103</span> <span>$str</span>.="<font color='#555555'>首页</font>"<span>; </span><span>104</span> <span>$str</span>.=" "<span>; </span><span>105</span> <span>if</span>(<span>$this</span>->nowpage!=1<span>) </span><span>106</span> <span>$str</span>.="<a href=".<span>$_SERVER</span>['PHP_SELF']."?page=".(<span>$this</span>->nowpage-1)."&page_type=".<span>$anothersearchstr</span>."¶meter2=".<span>$anothersearchstrs</span>." class=".<span>$class</span>.">上一页</a>"<span>; </span><span>107</span> <span>else</span> <span>108</span> <span>$str</span>.="<font color='#555555'>上一页</font>"<span>; </span><span>109</span> <span>$str</span>.=" "<span>; </span><span>110</span> <span>if</span>(<span>$this</span>->nowpage!=<span>$pagecount</span><span>) </span><span>111</span> <span>$str</span>.="<a href=".<span>$_SERVER</span>['PHP_SELF']."?page=".(<span>$this</span>->nowpage+1)."&page_type=".<span>$anothersearchstr</span>."¶meter2=".<span>$anothersearchstrs</span>." class=".<span>$class</span>.">下一页</a>"<span>; </span><span>112</span> <span>else</span> <span>113</span> <span>$str</span>.="<font color='#555555'>下一页</font>"<span>; </span><span>114</span> <span>$str</span>.=" "<span>; </span><span>115</span> <span>if</span>(<span>$this</span>->nowpage!=<span>$pagecount</span><span>) </span><span>116</span> <span>$str</span>.="<a href=".<span>$_SERVER</span>['PHP_SELF']."?page=".<span>$pagecount</span>."&page_type=".<span>$anothersearchstr</span>."¶meter2=".<span>$anothersearchstrs</span>." class=".<span>$class</span>.">尾页</a>"<span>; </span><span>117</span> <span>else</span> <span>118</span> <span>$str</span>.="<font color='#555555'>尾页</font>"<span>; </span><span>119</span> <span>if</span>(<span>count</span>(<span>$this</span>-><span>array</span>)==0 || <span>$this</span>-><span>array</span>==<span>false</span><span>) </span><span>120</span> <span>return</span> "无数据!"<span>; </span><span>121</span> <span>else</span> <span>122</span> <span>return</span> <span>$str</span><span>; </span><span>123</span> <span> } </span><span>124</span> <span>} </span><span>125</span> <span>//</span><span>系统常用方法</span> <span>126</span> <span>class</span><span> UseFun{ </span><span>127</span> <span>128</span> <span>function</span> UnHtml(<span>$text</span><span>){ </span><span>129</span> <span>$content</span>=(<span>nl2br</span>(<span>htmlspecialchars</span>(<span>$text</span><span>))); </span><span>130</span> <span>$content</span>=<span>str_replace</span>("[strong]","<strong>",<span>$content</span><span>); </span><span>131</span> <span>$content</span>=<span>str_replace</span>("[/strong]","</strong>",<span>$content</span><span>); </span><span>132</span> <span>$content</span>=<span>str_replace</span>("[em]","<em>",<span>$content</span><span>); </span><span>133</span> <span>$content</span>=<span>str_replace</span>("[/em]","</em>",<span>$content</span><span>); </span><span>134</span> <span>$content</span>=<span>str_replace</span>("[u]","<u>",<span>$content</span><span>); </span><span>135</span> <span>$content</span>=<span>str_replace</span>("[/u]","</u>",<span>$content</span><span>); </span><span>136</span> <span>137</span> <span>138</span> <span>$content</span>=<span>str_replace</span>("[font color=#FF0000]","<font color=#FF0000>",<span>$content</span><span>); </span><span>139</span> <span>$content</span>=<span>str_replace</span>("[font color=#00FF00]","<font color=#00FF00>",<span>$content</span><span>); </span><span>140</span> <span>$content</span>=<span>str_replace</span>("[font color=#0000FF]","<font color=#0000FF>",<span>$content</span><span>); </span><span>141</span> <span>142</span> <span>$content</span>=<span>str_replace</span>("[font face=楷体_GB2312]","<font face=楷体_GB2312>",<span>$content</span><span>); </span><span>143</span> <span>$content</span>=<span>str_replace</span>("[font face=宋体]","<font face=新宋体>",<span>$content</span><span>); </span><span>144</span> <span>$content</span>=<span>str_replace</span>("[font face=隶书]","<font face=隶书>",<span>$content</span><span>); </span><span>145</span> <span>$content</span>=<span>str_replace</span>("[/font]","</font>",<span>$content</span><span>); </span><span>146</span> <span>//</span><span>$content=str_replace(chr(32)," ",$content);</span> <span>147</span> <span>$content</span>=<span>str_replace</span>("[font size=1]","<font size=1>",<span>$content</span><span>); </span><span>148</span> <span>$content</span>=<span>str_replace</span>("[font size=2]","<font size=2>",<span>$content</span><span>); </span><span>149</span> <span>$content</span>=<span>str_replace</span>("[font size=3]","<font size=3>",<span>$content</span><span>); </span><span>150</span> <span>$content</span>=<span>str_replace</span>("[font size=4]","<font size=4>",<span>$content</span><span>); </span><span>151</span> <span>$content</span>=<span>str_replace</span>("[font size=5]","<font size=5>",<span>$content</span><span>); </span><span>152</span> <span>$content</span>=<span>str_replace</span>("[font size=6]","<font size=6>",<span>$content</span><span>); </span><span>153</span> <span>154</span> <span>$content</span>=<span>str_replace</span>("[FIELDSET][LEGEND]","<FIELDSET><LEGEND>",<span>$content</span><span>); </span><span>155</span> <span>$content</span>=<span>str_replace</span>("[/LEGEND]","</LEGEND>",<span>$content</span><span>); </span><span>156</span> <span>$content</span>=<span>str_replace</span>("[/FIELDSET]","</FIELDSET>",<span>$content</span><span>); </span><span>157</span> <span>return</span> <span>$content</span><span>; </span><span>158</span> <span> } </span><span>159</span> <span>160</span> <span>} </span><span>161</span> <span>162</span> ?></span>