当前位置: 编程技术>php
本页文章导读:
▪解决PHP上传大文件出现错误的问题 在php中,PHP配置文件对文件上传大小限制值较小,所以我们要修改下upload_max_filesize,默认为2M,将其设置大一点。
然后再修改下max_execution_time最大上传时间,将其默认值30(秒)改大点。
完.........
▪php调用google天气api的实例代码 php调用google的天气API,实时显示天气信息。
代码:
<?php
/**
* 显示天气信息
* 文件名:google_weather_api.php
* by www.
*/
class weather
{
public static $response;
public static $location;
public s.........
▪php mysql搜索类(附实例) 1,php\mysql搜索类
<?php
/**
* mysql搜索类,可自定义查询条件
* by www.
*/
class search {
var $table;
var $field1;
var $field2;
function queryRow($query){
//定义数据库配置信息
defi.........
[1]解决PHP上传大文件出现错误的问题
来源: 互联网 发布时间: 2013-12-24
在php中,PHP配置文件对文件上传大小限制值较小,所以我们要修改下upload_max_filesize,默认为2M,将其设置大一点。
然后再修改下max_execution_time最大上传时间,将其默认值30(秒)改大点。
完成以上两项的设置后,有些没有超过upload_max_filesize的大文件,有时还是不能正常上传,并且php代码中也没有什么错误提示。
原因在于:POST数据超过了最大POST数据值post_max_size,默认为8M,也设置大一点。
综上,修改php.ini文件的三个值:
代码示例:
max_execution_time = 30
post_max_size = 8M
upload_max_filesize = 2M
post_max_size = 8M
upload_max_filesize = 2M
重启Web Server,OK!
对于,有错误但没有提示消息的情况,可以开启PHP的日志文件,记录好错误日志,以备遇到问题时查看。
将日志写入指定文件:
error_log = "/path/to/error.log"
windows系统的话,可以将日志写入系统日志:
error_log = syslog
[2]php调用google天气api的实例代码
来源: 互联网 发布时间: 2013-12-24
php调用google的天气API,实时显示天气信息。
代码:
<?php /** * 显示天气信息 * 文件名:google_weather_api.php * by www. */ class weather { public static $response; public static $location; public static $current; public static $nextdays; public static $error = false; public function weather() { $this->location = 'Brasov'; } public function get() { if (empty($this->location)) { $this->error = true; return false; } $requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en"; $xml_str = file_get_contents($requestAddress,0); $xml = new SimplexmlElement($xml_str); if (!$xml->weather->problem_cause) { $this->response = $xml->weather; $this->parse(); }else{ $this->error = true; } } public function parse() { foreach($this->response as $item) { $this->current = $item->current_conditions; foreach($item->forecast_conditions as $new) { $this->nextdays[] = $new; } } } public function display() { foreach($this->nextdays as $new) { echo '<div >'; echo '<h2>'.$new->day_of_week['data'].'</h2>'; echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>'; echo '<br />Min: '.$this->convert($new->low['data']).' ℃'; echo '<br />Max: '.$this->convert($new->high['data']).' ℃'; echo '</div>'; } } public function convert($value, $unit = "C"){ switch($unit){ case "C": return number_format(($value - 32)/1.8); break; case "F": return round($value * 1.8 + 32); break; default: return $value; break; }; } }
调用示例:
<?php require_once('google_weather_api.php'); $weather = new weather(); if (!empty($_GET['loc'])) { $weather->location = $_GET['loc']; } $weather->get(); if($weather->error){ die('We couldn\'t find your location.'); }else{ echo ' <div id="currentWeather"> <h1>现在是:'.ucwords($weather->location).': '.$weather->current->temp_c['data'].' ℃</h1> <img src="http://www.google.com/' .$weather->current->icon['data'] . '"/> <p>'.$weather->current->condition['data'].'</p> <p>'.$weather->current->humidity['data'].'</p> <p>'.$weather->current->wind_condition['data'].'</p> </div> '; // 显示更多天气信息 // print_r($weather->nextdays); $weather->display(); }您可能感兴趣的文章:
php 天气预报代码一例
php调用yahoo sina api天气预报的实现代码
Google API 获取当前天气信息的php代码
[3]php mysql搜索类(附实例)
来源: 互联网 发布时间: 2013-12-24
1,php\mysql搜索类
<?php /** * mysql搜索类,可自定义查询条件 * by www. */ class search { var $table; var $field1; var $field2; function queryRow($query){ //定义数据库配置信息 define("host", "localhost"); define("login", "root"); define("senha", ""); //定义数据库名称 define("data", "teste"); //连接数据库 try{ $host = host; $data = data; $connection = new PDO("mysql:host=$host;dbname=$data", login, senha); //$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $result = $connection->prepare($query); $result->execute(); return $result; $this->connection = $connection; }catch(PDOException $e){ echo $e->getMessage(); } } function close($connection){ $connection = null; } function query($query){ $host = host; $result = $this->queryRow($query); $row = $result->fetch(PDO::FETCH_ASSOC); $this->close($this->connection); $this->query = $query; return $row; } //结束连接 //显示数据字段内容 function fieldSelect(){ $query = $this->queryRow('SHOW FULL COLUMNS FROM '.$this->table); $retorno = "<select name=\"fieldselect\">\n"; foreach ($query as $collums){ if ($_POST['fieldselect'] == $collums['Field']){ $selected = " selected=\"selected\" "; }else{ $selected = ""; } $retorno .= "<option value=\"$collums[Field]\"$selected>$collums[Field]</option>\n"; } $retorno .= "</select>\n"; return $retorno; } //构造查询条件 function whereSelect(){ $wheres = array(); $wheres[] = 'equal'; $wheres[] = 'diferent'; $wheres[] = 'minor'; $wheres[] = 'more'; $wheres[] = 'minororequal'; $wheres[] = 'moreorequal'; $wheres[] = 'content'; $wheres[] = 'notcontent'; $wheres[] = 'between'; $wheres[] = 'notbetween'; $label[] = 'Equal'; $label[] = 'Diferent'; $label[] = 'Minor'; $label[] = 'More'; $label[] = 'Minor or Equal'; $label[] = 'More or Equal'; $label[] = 'Content'; $label[] = 'Not Content'; $label[] = 'Between'; $label[] = 'Not Between'; $retorno = "<select name=\"select\">\n"; $i=0; do{ if ($_POST['select'] == $wheres[$i]){ $selected = " selected=\"selected\" "; }else{ $selected = ""; } $retorno .= "<option value=\"$wheres[$i]\"$selected>$label[$i]</option>\n"; $i++; }while($i < count($wheres)); $retorno .= "</select>\n"; return $retorno; } function fieldText($size, $max){ $retorno .= "<input type=\"text\" name=\"fieldtext\" size=\"$size\" maxlength=\"$max\" value=\"$_POST[fieldtext]\" />\n"; return $retorno; } /构造条件与变量查询 function wheres($value){ $retorno = ""; //parei aqui $this->field2 = explode(' OR ',$this->field2); //var_dump($this->field2); $i = 0; switch($value){ case 'equal': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 = '$field2' "; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'diferent': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 != '$field2'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'minor': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 < '$field2'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'more': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 > '$field2'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'minororequal': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 <= '$field2'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'moreorequal': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 >= '$field2'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'content': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 LIKE '%$field2%'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'notcontent': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 NOT LIKE '%$field2%'"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'between': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 BETWEEN $field2"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; case 'notbetween': foreach ($this->field2 as $field2){ $retorno .= "$this->field1 NOT BETWEEN $field2"; $i = ++$i; if ($i != 0 && $i != count($this->field2)){ $retorno .= " OR "; } } break; } return $retorno; } //输出查询结果 function result($fields){ if (isset($_POST['submit'])){ $this->field1 = $_POST['fieldselect']; $this->field2 = $_POST['fieldtext']; $resultfields = ""; if(is_array($fields)){ $i = 0; foreach($fields as $collums){ if($i< count($fields)-1){ $resultfields .= $collums.', '; }else{ $resultfields .= $collums; } $i = ++$i; } }else{ $resultfields = $fields; } $query = $this->queryRow("SELECT $resultfields FROM $this->table WHERE ".$this->wheres($_POST['select'])); $retorno = "<table>\n"; foreach($query as $querycollum){ $retorno .= "<tr>"; if(is_array($fields)){ foreach($fields as $collumstable){ $retorno .= "<td>$querycollum[$collumstable]</td>"; } $retorno .= "</tr>\n"; } } $retorno .= "</table>\n"; return $retorno; } } } ?>
2,调用示例:
<?php include('search.class.php'); $search = new search; //数据表 $search->table = 'cidades'; //数组形式的结果 $result = array('id', 'Regiao'); ?> <p> 插入一条数据用于查询测试。</p> <p> 可以测试:between, not between, AND 等操作符。</p> <form action="/blog_article/</$_SERVER[.html'PHP_SELF']?>" method="post"> <?=$search->fieldSelect()?> <?=$search->whereSelect()?> <?=$search->fieldText(10,20)?> <input type="submit" name="submit" value="提交查询" /> </form> <?=$search->result($result)?>
3,附:sql代码:
CREATE TABLE IF NOT EXISTS `cidades` ( `id` int(11) NOT NULL auto_increment, `Regiao` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ; -- -- Extraindo dados da tabela `cidades` -- INSERT INTO `cidades` (`id`, `Regiao`) VALUES (1, 'REGI?O METROPOLITANA'), (2, 'MATA NORTE'), (3, 'MATA SUL'), (4, 'AGRESTE SETENTRIONAL'), (5, 'AGRESTE CENTRAL'), (6, 'AGRESTE MERIDIONAL'), (7, 'MOXOT'), (8, 'PAJE'), (9, 'ITAPARICA'), (10, 'SERT?O CENTRAL'), (11, 'S?O FRANCISCO'), (12, 'ARARIPE');
最新技术文章: