zhengsenlin888 发表于 2011-12-22 08:54

php连接mysql

<div>&lt;?php</div><div>$ver = '2011090989';</div><div><br></div><div>$db_host = "192.168.1.4";</div><div>$db_port = 8808;</div><div>$db_name = "fivetrees_manage";</div><div>$db_user = "root";</div><div>$db_pass = '123456';</div><div><br></div><div>$db = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);</div><div><br></div><div>if (mysqli_connect_errno()) {</div><div>&nbsp; &nbsp; printf("Connect failed: %s\n", mysqli_connect_error());</div><div>&nbsp; &nbsp; exit();</div><div>}</div><div><br></div><div>$sql = "</div><div>SELECT</div><div>&nbsp; `name` AS `id`,</div><div>&nbsp; `api_server` AS `node`,</div><div>&nbsp; `db_server` AS `db`</div><div>FROM</div><div>&nbsp; `servers`</div><div>WHERE</div><div>&nbsp; `cid` != 2 AND</div><div>&nbsp; `open` = 1 AND</div><div>&nbsp; `server_ver` != '$ver'</div><div>ORDER BY</div><div>&nbsp; `db_server`, `sid` ASC</div><div>";</div><div><br></div><div>$plan = '';</div><div>$plan_id = 0;</div><div>$last_db = '';</div><div>$counts = array();</div><div><br></div><div>if ($result = $db-&gt;query($sql)) {</div><div>&nbsp; while($row = $result-&gt;fetch_array(MYSQLI_ASSOC)) {</div><div>&nbsp; &nbsp; $server = substr($row['node'], 0, 5);</div><div>&nbsp; &nbsp; $code = "./shell-update {$row['id']} $server $ver\n";</div><div><br></div><div>&nbsp; &nbsp; if ($last_db != $row['db']) {</div><div>&nbsp; &nbsp; &nbsp; $plan_id= 1;</div><div>&nbsp; &nbsp; &nbsp; $last_db = $row['db'];</div><div>&nbsp; &nbsp; &nbsp; $counts[$plan_id] = 0;</div><div>&nbsp; &nbsp; &nbsp; file_put_contents("update_plan_$plan_id.sh", "");</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; $counts[$plan_id]= 1;</div><div><br></div><div>&nbsp; &nbsp; file_put_contents("update_plan_$plan_id.sh", $code, FILE_APPEND);</div><div>&nbsp; }</div><div><br></div><div>&nbsp; $result-&gt;close();</div><div>}</div><div><br></div><div>$db-&gt;close();</div><div><br></div><div>print_r($counts);</div><div>?&gt;</div><div><br></div>
页: [1]
查看完整版本: php连接mysql