phpenum
1. php計算問題,幫我看看怎麼計算這個比例值
不要在循環之間去累計計算總數,這樣是無法計算比例的。
在循環輸出之前 使用select sum(votenum)。。。提前計算出總數。
然後比例使用 row3[votenum]/總數 來計算
2. php頁面如何轉換mysql中的enum類型
/*搜索的語句,如果表名稱為table1*/
$sql = "select locks from table1 where id=1";
$que = mysql_query($sql);
$row = mysql_fetch_array($que);
$re = ($row['locks'] == '0') ? '禁用' : '不禁用';
echo $re;
3. php 怎麼獲得mysql資料庫欄位枚舉
Array ( [0] => 1 [1] => storylok [2] => lele1989 [3] => ? )
為什麼獲取到的這個枚舉類型是問號呢? 我在mysql控制台看的確是中文字呢。 字元集PHP和mysql都是utf8,怎麼獲取到這個枚舉類型的值。
<?php
header ( "Content-Type:text/html; charset=utf-8" );
$host = "localhost";
$user = "root";
$password = "";
@$conn = mysql_connect($host,$user,$password) or die("資料庫連接失敗,請檢查資料庫配置!" . mysql_error());
$tableName = "viuoo";
mysql_select_db($tableName,$conn) or die('資料庫表選擇出錯,請檢查資料庫表名是否正確!');
mysql_query("SET NAMES 'utf-8'");
$dm = "SELECT * FROM user";
$result = mysql_query($dm,$conn);
while($res = mysql_fetch_row($result)){
print_r($res);
}
?>
mysql php utf8 資料庫
------解決方案--------------------
mysql_query("SET NAMES 'utf-8'");
改成
mysql_query("SET NAMES 'UTF8'");
4. 如何設置php thrift 超時時間
最近需要用到Thrift介面,
是Facebook開發的apache開源項目,公司要用,研究了一下
所以寫了個PHP調用Thrift的方法事例
以下是代碼,以免以後別人再走彎路
我是在Yii框架中實現的,和原生代碼應該是一樣的
官方下載包里也有PHP調用Thrift的例子
<?php
/**
* @author fzxa
* @create 2012/05/22
Thrift推薦職位相關介面
注:詳細說明文檔在 \protected\components\thrift\推薦系統API.docx
@desc 說明Thrift介面數據格式:
EntityType介面數據格式
enum EntityType {
POSITION = 0,
RESUME = 1
}
EntityInfo介面數據格式
struct EntityInfo {
1: EntityType type,
2: string id, // 實體id
3: double rank, // rank值
4: string address, // 工作地點
5: i32 salary, // 薪水
6: string instry, // 行業類別
7: string profession, // 職業類別
8: i32 workingage, // 工作年限
9: i32 degree, // 學歷
10: string time, // 過期時間或更新時間
11: map<string,string> descriptions, // 文字描述,其中key為欄位名,value為欄位內容
12: map<string,i32> tags, // 技能標簽
}
ResultInfo介面數據格式
struct ResultInfo {
1: EntityType type,
2: string id, // 實體id
3: i32 score, // 推薦分數,取值范圍:[0, 100]
}
*/
$GLOBALS['THRIFT_ROOT'] = $_SERVER['DOCUMENT_ROOT'].'/p/protected/components/thrift';
require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );
//生成的文件 RecommendService.php
require_once( $GLOBALS['THRIFT_ROOT'] . '/RecommendService.php' );
//ini_set('display_errors', E_ALL);
class ThriftInterface
{
private static $thriftHost = '*.*.*.*'; //Thrift介面伺服器IP
private static $thriftPort = 8080; //Thrift埠
/**
* 建立Thrift連接
* @return boolean
*/
private static function client(){
$socket = new TSocket(self::$thriftHost, self::$thriftPort);
$socket->setSendTimeout(10000);
$socket->setRecvTimeout(20000);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new RecommendServiceClient($protocol);
$transport->open();
$socket->setDebug(TRUE);
return $client;
}
/**
* 獲取職位推薦列表ById
*
* @param Number $type 0:代表職位,1:代表簡歷
* @param Number $id 實體ID
* @param Array 推薦結果列表
* @example: $Recommend = ThriftInterface::getRecommendedResultById('1','1982');
*/
public static function getRecommendedResultById($type,$id){
$client = self::client();
$ResultById = $client->getRecommendedResultById($type, $id);
return $ResultById;
}
/**
* 獲取推薦列表ByEntity
* @param EntityInfo $entity 職位或簡歷實體信息
*/
public static function getRecommendedResultByEntity($entity){
$client = self::client();
$EntityInfo = new EntityInfo();
$EntityInfo->type = (string)$entity['type'];
$EntityInfo->id = (string)$entity['id'];
$EntityInfo->rank = (float)$entity['rank'];
$EntityInfo->address = (string)$entity['address'];
$EntityInfo->salary = (int)$entity['salary'];
$EntityInfo->instry = (string)$entity['instry'];
$EntityInfo->profession = (string)$entity['profession'];
$EntityInfo->workingage = (int)$entity['workingage'];
$EntityInfo->degree = (int)$entity['degree'];
$EntityInfo->time = (string)$entity['time'];
$EntityInfo->descriptions = (array)$entity['descriptions'];
$EntityInfo->tags = (array)$entity['tags'];
$ResultByEntity = $client->getRecommendedResultByEntity($EntityInfo);
return $ResultByEntity;
}
/**
* 計算任一簡歷與職位的匹配分數ById
*
* @param Number $type1 0:代表職位,1:代表簡歷
* @param Number $id1 實體ID
* @param Number $type2 0:代表職位,1:代表簡歷
* @param Number $id2 實體ID
* @example Number
*/
public static function getRecommendedScoreById($type1, $id1, $type2, $id2){
$client = self::client();
$ScoreById = $client->getRecommendedScoreById($type1, $id1, $type2, $id2);
return $ScoreById;
}
/**
* 計算任一簡歷與職位的匹配分數ByEntity
*
* @param EntityInfo $entity 實體的所有信息
* @param EntityType $type2
* @param string $id2 實體ID2
* @return i32 簡歷與職位的推薦分數,取值范圍:[0, 100]
*/
public static function getRecommendedScoreByEntity($entity, $type2, $id2){
$client = self::client();
$EntityInfo = new EntityInfo();
$EntityInfo->type = (string)$entity['type'];
$EntityInfo->id = (string)$entity['id'];
$EntityInfo->rank = (float)$entity['rank'];
$EntityInfo->address = (string)$entity['address'];
$EntityInfo->salary = (int)$entity['salary'];
$EntityInfo->instry = (string)$entity['instry'];
$EntityInfo->profession = (string)$entity['profession'];
$EntityInfo->workingage = (int)$entity['workingage'];
$EntityInfo->degree = (int)$entity['degree'];
$EntityInfo->time = (string)$entity['time'];
$EntityInfo->descriptions = (array)$entity['descriptions'];
$EntityInfo->tags = (array)$entity['tags'];
$ResultInfo = new ResultInfo();
$ResultInfo->type = (string)$type2['type'];
$ResultInfo->id = (string)$type2['id'];
$ResultInfo->score = (int)$type2['score'];
$ScoreByEntity = $client->getRecommendedScoreByEntity($EntityInfo, $type2, $id2);
return $ScoreByEntity;
}
。