开发者

How can i improve this code? (implode/explode/unserialize)

Im not satisfied im doing this the best way possible. Im taking serialized code from database column location. Im assuming that i have to implode the se开发者_开发知识库rialized data and then explode it because when i just use implode i cant access the entries in counties so i used explode. Im sure there's a better approach?

$adid = $_GET['id'];
$query = "SELECT * FROM ad WHERE id = '$adid'";
$data = mysqli_query($dbc, $query);
$ad = mysqli_fetch_array($data);



 //retrieve serialized data containing county id's
    $counties = explode(" ", implode(" ", unserialize($ad['location'])));

for ($i = 0; $i <= count($counties); $i++) {
echo getCounty($counties[$i]); //getCounty: enter county id and return county name
}


FYI: $ad['location'] = a:2:{i:0;s:1:"2";i:1;s:2:"20";}


Don't use implode(), when you have a serialized string, unserialize() only does the trick.


unserialize() should return an array. Why are you imploding and then exploding?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜