Reputation:
I am new to php and 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. For smaller states with a few hospitals, the query works fine in 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>"
?>
Upvotes: 0
Views: 740
Reputation: 10033
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:
mysql_select_db(it_phys);
unless it_phys is a constant, it should be quoted
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($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 ($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:
$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`";
Upvotes: 2
Reputation: 23
As the page is generated on the server, the query can't "trash" the IE. Perhaps it's the HTML that you generate.
Some things I noticed while scanning through your code:
Upvotes: 0
Reputation: 5561
It is possible that your PHP script is timing out before the query finishes retrieving all the records. 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.
Upvotes: 0
Reputation: 2257
It probably has nothing to do with 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.
Data inside links should be escaped using urlencode()
. Data inside attributes or otherwise should probably be escaped using htmlspecialchars()
unless you know its safe to not do so.
(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.)
Upvotes: 0