简体   繁体   English

使用Internet Explorer时,MySQL查询有时会失败

[英]MySQL query fails sometimes when using Internet Explorer

I am new to php and MySQL. 我是php和MySQL的新手。 I have a site where a user can choose a state and display hospitals in that state. 我有一个网站,用户可以在其中选择一个州并在该州显示医院。 It works well in FireFox for all states, but in IE, when I have state with a couple hundred hospitals, the screen flashes and I eventually get a message that the page cannot be displayed. 它在FireFox中对所有州都适用,但是在IE中,当我有几百家医院的州时,屏幕闪烁,最终我收到一条消息,指出该页面无法显示。 For smaller states with a few hospitals, the query works fine in IE. 对于有几家医院的较小州,该查询在IE中工作正常。

Again, I am new to this so any suggestions would be greatly appreciated. 再次,我是新来的,所以任何建议将不胜感激。 Here's the code: 这是代码:

<form action="redirect_hosp.php" method="get">

<?php

$link = mysql_connect('SERVERNAME.com', 'USERNAME', 'PASSWORD'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
/*print 'Connected successfully';*/ 
mysql_select_db(it_phys);

$sql = "SELECT distinct(state) FROM hospitals ORDER BY state";
$rs = mysql_query($sql)or die("Connection to DataBase failed");
print ("<div align=center>");
print("<font color='#008000' size='2' face='Tahoma'><b>Find a Hospital</b></font><br><br>");
print ("Select a State<br>");
print ("<SELECT name='State' onchange='form.submit();'>");
print("<OPTION value='none' selected></OPTION>\n");
for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print("<OPTION value=\"$tmp[0]\">$tmp[0]</OPTION>\n");
}
print ("</SELECT>");
print ("</div>");
mysql_free_result($rs);
mysql_close($link);

?>


<?php
if(isset($_GET['State'])) 
{
  $State=$_GET['State'];
  print "<div align=center><br><br>";
  print "State Selected: ".$State;
  print "<br><br></div>";
  /* run query to pull members based on state */
  $link = mysql_connect('SERVERNAME.com', 'USERNAME', 'PASSWORD'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
/*print 'Connected successfully';*/ 
mysql_select_db(it_phys);

      $query = "SELECT
           hospitals.`Hospital Name`, hospitals.Address1,
           concat(rtrim(hospitals.City),', ',rtrim(upper(hospitals.State)), ' ', hospitals.zip) as City_State,
           hospitals.state,hospitals.`Phone Number`,hospitals.`Hospital Type`,hospitals.`Emergency Service`,
           hospitals.map
           FROM hospitals WHERE hospitals.state='".$State."' ORDER by hospitals.`Hospital Name`"; 


    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
  /* show in table */
  // Printing results in HTML
    print "<style>

    h2 { border-bottom: 3px solid red; }
    table { border-bottom: 1px solid blue; align='left'; cellpadding=0; cellspacing=0 }
    td {border-bottom: 0px outset black; cellpadding=0 }

    </style>";
    print "<div align=center>";
    print "<table cellpadding='20'>";


    $i=0;
    $rows=mysql_num_rows($result);
    while($i < $rows) {
        print "<tr>";
        print "<style> td {border-bottom: 3px outset green; }</style>";
        print "<td valign='top' align='left'>";
        print "<font color='red'><strong>".mysql_result($result, $i, 0)."</strong></font>";
        print "<br>".mysql_result($result, $i, 1);
        print "<br><font color='red'>".mysql_result($result, $i, 2)."</font>";
        print "<br>".mysql_result($result, $i, 4);  
        print "</td>";
        print "<td width=350 valign='top' align='left'>";
        print "<br>Type: ".mysql_result($result, $i, 5);
        print "<br>Emergency Care: ".mysql_result($result, $i, 6);
        print "<br><br>";
        print "<a STYLE='text-decoration:none' target='_blank'
                 href='http://maps.google.com/maps?f=q&source=s_q&hl=en&q="
                .mysql_result($result, $i, 7)."'><b> Show Map</b></a>";     
        print "</td>";
        print "</tr>";

        $i=$i+1;

    }
mysql_free_result($result);
mysql_close($link);
}
print "</table>"
?> 

It might be this part here: 这可能是这部分:

print "<tr>";
print "<style> td {border-bottom: 3px outset green; }</style>";
print "<td valign='top' align='left'>";

You should move the style data up to your previous style tag. 您应该将样式数据上移到先前的样式标签。

And about your PHP code: 关于您的PHP代码:

mysql_select_db(it_phys);

unless it_phys is a constant, it should be quoted 除非it_phys是一个常量,否则应将其用引号引起来

mysql_select_db('it_phys');

Instead of the for loop which calculates the number of returned rows N + 1 times where N is the number of returned rows, 代替for循环,它计算返回的行数N +1次,其中N是返回的行数,

for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print("<OPTION value=\"$tmp[0]\">$tmp[0]</OPTION>\n");
}

try this while loop (and use brackets around arrays): 试试这个while循环(并在数组周围使用方括号):

while ($tmp = mysql_fetch_row($rs)) {
    print("<OPTION value=\"{$tmp[0]}\">{$tmp[0]}</OPTION>\n");
}

And escape all input going to your database with mysql_real_escape_string: 并使用mysql_real_escape_string转义所有输入数据库的输入:

$query = "SELECT
           `Hospital Name`, Address1,
           concat(rtrim(hospitals.City),', ',rtrim(upper(State)), ' ', zip) as City_State,
           state,`Phone Number`,`Hospital Type`,`Emergency Service`,
           map
           FROM hospitals WHERE state='".mysql_real_escape_string($State)."' ORDER by `Hospital Name`"; 

It probably has nothing to do with MySQL. 它可能与MySQL无关。 It's difficult to say without looking at the actual page (link?), but you are outputting a lot of raw unescaped database data inside tag attributes and other places which may cause IE's parser to barf. 不查看实际页面(链接?)很难说,但是您在标记属性和其他可能会导致IE解析器阻塞的其他位置输出了大量未转义的原始数据库数据。

Data inside links should be escaped using urlencode() . 链接中的数据应使用urlencode()进行转义。 Data inside attributes or otherwise should probably be escaped using htmlspecialchars() unless you know its safe to not do so. 除非您知道这样做是安全的,否则应该使用htmlspecialchars()属性内的数据或其他数据进行转义。

(That said, "Page Cannot Be Displayed" can be caused by lots of things, a few of them being early DOM access, add-ons, cache etc. They can be quite difficult to track down.) (也就是说,“页面无法显示”可能是由很多原因引起的,其中一些原因是早期的DOM访问,加载项,缓存等。很难追踪。)

It is possible that your PHP script is timing out before the query finishes retrieving all the records. 在查询完成检索所有记录之前,您的PHP脚本可能已超时。 What is your script timeout set to? 您的脚本超时设置为什么?

Also, you need to learn how to separate your data from your logic and your logic from your presentation. 此外,您还需要学习如何将数据与逻辑分离,并将逻辑与演示分离。

As the page is generated on the server, the query can't "trash" the IE. 由于页面是在服务器上生成的,因此查询无法“破坏” IE。 Perhaps it's the HTML that you generate. 也许是您生成的HTML。

Some things I noticed while scanning through your code: 我在扫描您的代码时注意到了一些事情:

  • You don't need to connect for every query. 您不需要为每个查询都连接。 Simply open one connection and close it in the end. 只需打开一个连接并最后将其关闭。
  • Is this a legacy database? 这是旧版数据库吗? Fields with spaces like this "Hospital Name" is somewhat shocking to me … 像这样的“医院名称”这样的字段使我有些震惊……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM