Home > Backend Development > PHP Tutorial > 小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码

小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码

WBOY
Release: 2016-06-23 13:35:42
Original
921 people have browsed it

<?$db_host = 'localhost';$db_user = 'root';$db_password = 'root3306';$db_database = 'chuyu';$con = mysql_connect($db_host, $db_user, $db_password);mysql_query("set names 'gb2312'"); if(!$con){die('数据库连接失败: ' . mysql_error());}mysql_select_db($db_database);global $site;$site_data=mysql_query("select * from dh_member_control where id=1",$con);$site=mysql_fetch_array($site_data);$hot_vip_data=mysql_query("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12",$con);$hot_vip=mysql_fetch_array($hot_vip_data);$i=1;while ($hot_vip && $i<=12){if ($i % 6 == 0){$last=" class=\"last\"";}else{$last="";}$i++;?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hot_vip["id"]?>.html'"><div class="vpc"><img  src="<? echo $hot_vip["photo"]? alt="小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码" >" /></div><div class="vpn"><? if (strlen($hot_vip["unick"])>4){echo substr($hot_vip["unick"],1,4);}else{echo $hot_vip["unick"];}if ($hot_vip["mpower"]===2){ echo "<a href=\"/vip/\" title=\"初遇会员\"><img  src=\"/images/v.png\" / alt="小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hot_vip["sri"]) ;echo intval($timediff/(86400*365)) . "岁";echo $hot_vip["add"];?></div></li><? }?>
Copy after login


测试结果显示12条一样的记录,而且中文是乱码..


回复讨论(解决方案)

你的 $hot_vip=mysql_fetch_array($hot_vip_data); 在循环外,且只执行了一次
如果没有 $i<=12 的话 while 都死循环了

至于乱码因素很多,你先把循环问题解决了再说

$hot_vip= mysql_fetch_array($hot_vip_data);
$i=1;
while ($hot_vip && $i<=12)

红色部分需要方在while里执行。

乱码问题估计没有加

解决方法如下:

<?echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$db_host = 'localhost';$db_user = 'root';$db_password = 'root3306';$db_database = 'chuyu';$con = mysql_connect($db_host, $db_user, $db_password);mysql_query("set names 'gb2312'"); if(!$con){die('数据库连接失败: ' . mysql_error());}mysql_select_db($db_database);global $site;$site_data=mysql_query("select * from dh_member_control where id=1",$con);$site=mysql_fetch_array($site_data); $hot_vip_data=mysql_query("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12",$con);#$hot_vip=mysql_fetch_array($hot_vip_data);$i=1;while ($hot_vip=mysql_fetch_array($hot_vip_data) && $i<=12){if ($i % 6 == 0){$last=" class=\"last\"";}else{$last="";}$i++;?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hot_vip["id"]?>.html'"><div class="vpc"><img  src="<? echo $hot_vip["photo"]? alt="小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码" >" /></div><div class="vpn"><? if (strlen($hot_vip["unick"])>4){echo substr($hot_vip["unick"],1,4);}else{echo $hot_vip["unick"];}if ($hot_vip["mpower"]===2){ echo "<a href=\"/vip/\" title=\"初遇会员\"><img  src=\"/images/v.png\" / alt="小白,请指点以下程序怎么不是循环输出的,而且输出的中文还是乱码" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hot_vip["sri"]) ;echo intval($timediff/(86400*365)) . "岁";echo $hot_vip["add"];?></div></li><? }?>
Copy after login



谢谢两位

问题解决了,但是我有还有个小疑惑:

$hot_vip_data = mysql_query("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`'/images/nan.png' AND `photo`'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12",$con);
$hot_vip = mysql_fetch_array($hot_vip_data);
while ($hot_vip ){...}



$hot_vip_data = mysql_query("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`'/images/nan.png' AND `photo`'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12",$con);
while ($hot_vip = mysql_fetch_array($hot_vip_data)){...}

是一样的么?

不一样!
$hot_vip = mysql_fetch_array($hot_vip_data); //取回一条记录
while ($hot_vip ){...} //如果 $hot_vip 不为空的话,将是死循环

while ($hot_vip = mysql_fetch_array($hot_vip_data)){...} //循环读取每一条记录,直到 $hot_vip 为空(读完了)

不一样!
$hot_vip = mysql_fetch_array($hot_vip_data); //取回一条记录
while ($hot_vip ){...} //如果 $hot_vip 不为空的话,将是死循环

while ($hot_vip = mysql_fetch_array($hot_vip_data)){...} //循环读取每一条记录,直到 $hot_vip 为空(读完了)



 谢谢~
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template