是服务器的问题吗?(mysql)

upchina 2002-05-24 03:23:42
我做了一个购物车,
用的是新网的标准一型,
但是浏览时有时候会出现:
too many connection....
failed………………!!!!!! 的错误
有时还会block server!!!!

我不知道还有没有其他人租万网的空间,
出没出现过这种情况,

出现这种情况,是我的代码的问题吗?

我的代码基本上像这样:
$html='';
$query='SELECT * FROM `subject` where father_id=0 order by show_order';
if(!($result=mysql_query($query))){
echo mysql_error();
die();
} //end if
if($result){

$i=0;
while($sub=mysql_fetch_array($result)){
$topsubject[$i]['name']=$sub['name'];
$topsubject[$i]['id']=$sub['id'];
$i++;
}//end while
}//end if

for($j=0;$j<$i;$j++)
{
$query='SELECT id,name FROM `subject` where father_id='.$topsubject[$j]['id'];
if(!($result=mysql_query($query))){
echo mysql_error();
die();
} //end if
if($result){
$html.='<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<tr align="left" valign="top">
<td><img src="images/sub_'.$topsubject[$j]['id'].'.gif" width="174" height="15"></td>
</tr>
</table>
<table width="600" border="1" align="center" bordercolor="#3767A7" bgcolor="#F8F8F5" cellpadding="4" cellspacing="0" bordercolordark="#F8F8F5">
<tr bgcolor="#F8F8F5">
<td height="15">
<table width="580" border="0" align="center" cellspacing="2" cellpadding="2">';
while($sub=mysql_fetch_array($result)){
$html.='
<tr align="left" valign="top">
<td width="25%" height="2">
<p align="left"><img src="images/ad/vote_ar.gif" width="11" height="14">'.$sub['name'].'</p>
</td>';
if($sub=mysql_fetch_array($result))
{
$html.='<td width="25%" height="2">
<p align="left"><img src="images/ad/vote_ar.gif" width="11" height="14">'.$sub['name'].'</p>
</td>';
}else{
$html.='<td width="25%" height="2">
<p align="left"></p>
</td>';
}
if($sub=mysql_fetch_array($result))
{
$html.='<td width="25%" height="2">
<p align="left"><img src="images/ad/vote_ar.gif" width="11" height="14">'.$sub['name'].'</p>
</td>';
}else{
$html.='<td width="25%" height="2">
<p align="left"></p>
</td>';
}
if($sub=mysql_fetch_array($result))
{
$html.='<td width="25%" height="2">
<p align="left"><img src="images/ad/vote_ar.gif" width="11" height="14">'.$sub['name'].'</p>
</td>';
}else{
$html.='<td width="25%" height="2">
<p align="left"></p>
</td>';
}

$html.='
</tr>';

}//end while
$html.=' </table>
</td>
</tr>
</table>';
}//end if
}

请帮忙!谢谢!

...全文
47 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
upchina 2002-05-26
  • 打赏
  • 举报
回复
$query1='select id,name from book where id='.$itembookid;
while(list($n,$itembookid)=each($data)){
$query1.=' or id='.$itembookid;
}
$result1=mysql_query($query1) or die(mysql_error());

$i=0;
while(($book[$i]=mysql_fetch_array($result1))){$i++;}

我改成这样写了,速度好像有一些提升。
但是我这里本来就是宽带,看的不明显。
不知道是不是改这么弄?
upchina 2002-05-26
  • 打赏
  • 举报
回复
如果把多次query改成多次fetch就可以提高效率了吗?
upchina 2002-05-26
  • 打赏
  • 举报
回复
倒,这也行啊~
wasy 2002-05-26
  • 打赏
  • 举报
回复
不会啦
upchina 2002-05-26
  • 打赏
  • 举报
回复
多谢zyme

for($j=0;$j<$i;$j++) {
$query='SELECT id,name FROM `subject` where father_id='.$topsubject[$j]['id']; ....}

sql我不是很熟呢

是不是应该

for($j=0;$j<$i;$j++) {
$query='SELECT id,name FROM `subject` where father_id='.$topsubject[$j]['id']. or father_id='.$topsubject;}
类似的,把$topsubject[$j]['id']都连到一个query里?
然后一次query完?

我现在不太明白,query完后,fetch时是否还要link到mysqlserver?
多谢!

zyme 2002-05-24
  • 打赏
  • 举报
回复
其次too many connection....也说明了你程序的问题!
if(!($result=mysql_query($query))){ }就此没必要,不利于阅读。
用or die()就行了呀。如果真要,那是你想在{}内写更详细的信息??
while($sub=mysql_fetch_array($result)){ }
可用while($sub[$i]=mysql_fetch_array($result)){ }更好,不用担心体内语句多造成易错!
for($j=0;$j<$i;$j++) {
$query='SELECT id,name FROM `subject` where father_id='.$topsubject[$j]['id']; ....}不是好做法;这就是服务器出现too many connection....的原因吧。你这样会造成瞬时内N次服务联接。====其实有SQL语句能一次实现你的想法的(从第用二维组存数据到N次服务器联接)====为何不仔细看看SQL文档??
祝你做得更好!

zyme 2002-05-24
  • 打赏
  • 举报
回复
首先,你的程序很多地方应该优化,
不良好的写程序习惯!
wasy 2002-05-24
  • 打赏
  • 举报
回复
有可能是服務器數據庫連接數有限制,這個可以在php.ini裏面設置的

你換成用mysql_pconnect試一試
darzui 2002-05-24
  • 打赏
  • 举报
回复
你在命令行上连它的mysql数据库,测试一下
upchina 2002-05-24
  • 打赏
  • 举报
回复
if(!($dblink=mysql_connect($dbhost,$dbuser,$dbpass))){
printf("MySql Failed!!!!!\n");
printf("Error:".mysql_error());
die();
}
mysql_select_db($dbdatabase)
darzui 2002-05-24
  • 打赏
  • 举报
回复
你连接mysql的语句是什么?
upchina 2002-05-24
  • 打赏
  • 举报
回复
新网的空间到底如何?
有没有人用过?

有没有人遇到过类似的情况?
jekend 2002-05-24
  • 打赏
  • 举报
回复
Server 的配置問題

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧