<?php
$serverName = "127.0.0.1";
$connectionInfo = array("Database"=>"TestDB","UID"=>"test","PWD"=>"test");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn) {
echo "Connection established.<br>";
} else {
echo "Connection could not be established.<br>";
die(print_r(sqlsrv_errors(), true));
exit();
}
$sql = "select * from T_Employee";
$result = sqlsrv_query($conn,$sql);
$data = array();
while($row=sqlsrv_fetch_array($result)) {
$data[] = $row;
}
foreach($data as $p) {
echo $p['Name']."<br>";
}
sqlsrv_close($conn);
echo "<br> Done <br>";
echo date("Y-m-d h:i:s");
?>
2011-01-20
php自動產生隨機密碼
该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数用于生成随机整数,其中 $min – $max 为 ASCII 码的范围,这里取 33 -126 ,可以根据需要调整范围,如ASCII码表中 97 – 122 位对应 a – z 的英文字母,具体可参考 ASCII码表; chr ( int $ascii )函数用于将对应整数 $ascii 转换成对应的字符
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);
php防止sql注入函数-discuz版本
$magic_quotes_gpc = get_magic_quotes_gpc();
@extract(daddslashes($_COOKIE));
@extract(daddslashes($_POST));
@extract(daddslashes($_GET));
if(!$magic_quotes_gpc) {
$_FILES = daddslashes($_FILES);
}
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
@extract(daddslashes($_COOKIE));
@extract(daddslashes($_POST));
@extract(daddslashes($_GET));
if(!$magic_quotes_gpc) {
$_FILES = daddslashes($_FILES);
}
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
2011-01-19
JS判斷IE瀏覽器最簡短方法
- <script type='text/javascript'>
- // Option from Dean Edwards:
- var ie = /*@cc_on!@*/false;
- // Use the commented line:
- var ie//@cc_on=1;
- // Variation (shorter variable):
- var ie = '\v'=='v';
- / / Option to Gareth Hayes (former record-holder):
- var ie = !+"\v1";
- </script>
2010-12-29
PHP API調用BING翻譯
資料來源:http://www.kalvin.cn/study/php-api-call-bing-translation-results.html
<?php echo bingtrans('大家好,欢迎光临我的博客!'); //return: Hello, and welcome to my blog!
function bingtrans($str = '', $in = 'zh-CHS', $to = 'en', $api = 'C2D5477B17DD7DCB1391E6B4D3222F2DB8AFAE7D') {
if (empty($str)) {
return false;
}
$url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" . $api . "&text=" . urlencode($str) . "&from=" . $in . "&to=" . $to;
if (function_exists('curl_init')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
} else {
$res = @file_get_contents($url);
}
preg_match("~<string([^><]*?)>([\s\S]*?)<\/string>~i", $res, $ostr);
if (empty($ostr[2])) {
return false;
} else {
return htmlspecialchars_decode($ostr[2]);
}
} ?>
<?php echo bingtrans('大家好,欢迎光临我的博客!'); //return: Hello, and welcome to my blog!
function bingtrans($str = '', $in = 'zh-CHS', $to = 'en', $api = 'C2D5477B17DD7DCB1391E6B4D3222F2DB8AFAE7D') {
if (empty($str)) {
return false;
}
$url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" . $api . "&text=" . urlencode($str) . "&from=" . $in . "&to=" . $to;
if (function_exists('curl_init')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
} else {
$res = @file_get_contents($url);
}
preg_match("~<string([^><]*?)>([\s\S]*?)<\/string>~i", $res, $ostr);
if (empty($ostr[2])) {
return false;
} else {
return htmlspecialchars_decode($ostr[2]);
}
} ?>