Jump to content

Multiple connections to the same database?


BNole

Recommended Posts

I've been connecting to the same database 3 times on each web page. Should I be doing this a different way to optimize it? Thanks.

<head>	<title>Fire Coach Joe Tiller -- Fan The Fire</title>	<meta name="description" content="Visitor comments about Purdue football and Coach Joe Tiller." />	<link rel="stylesheet" type="text/css" href="../stylesheet.css" /></head><body>	<table align="center" width="760" cellpadding="0" cellspacing="10" bgcolor="#ffffff" style="border: 2px solid #000000">		<tr>			<td align="center">				<img src="../graphics/header.png" alt="Fire Coach Tiller & Co." />			</td>		</tr>		<tr>			<td>				<hr />				<table align="center" cellpadding="5" cellspacing="0">					<tr>						<td>							<a href="../index.html">								<img src="../graphics/buttons/home.png" border="0" alt="Home" />							</a>						</td>						<td>							<a href="../thetruth.html">								<img src="../graphics/buttons/thetruth.png" border="0" alt="The Truth" />							</a>						</td>						<td>							<a href="../inthepress.html">								<img src="../graphics/buttons/inthepress.png" border="0" alt="In The Press" />							</a>						</td>						<td>							<a href="../fightback.html">								<img src="../graphics/buttons/fightback.png" border="0" alt="Fight Back" />							</a>						</td>						<td>							<a href="../loveletters.html">								<img src="../graphics/buttons/loveletters.png" border="0" alt="Love Letters" />							</a>						</td>						<td>							<a href="index.php">								<img src="../graphics/buttons/fanthefire2.png" border="0" alt="Fan The Fire" />							</a>						</td>					</tr>				</table>				<hr />			</td>		</tr>		<tr>			<td>				<table align="center" width="600" cellpadding="10" cellspacing="0">					<tr>						<td>							<table align="center" cellpadding="0" cellspacing="0">								<tr>									<td align="center" width="100" />									<td align="center" width="200">										<?php											$con = mysql_connect("localhost","****","****");												if (!$con)													{														die('Could Not Connect: ' . mysql_error());													}												mysql_select_db("firecoac_firecoachtiller", $con);												$result = mysql_query("SELECT * FROM fanthefire");												$num_rows = mysql_num_rows($result);												echo '<i>There are</i> <b>';												echo $num_rows;												echo '</b> <i>comments.</i>';											mysql_close($con);										?>									</td>									<td align="center" width="100">										<a href="page2.php">											<img src="../graphics/arrownext.png" border="0" alt="Next Page" />										</a>									</td>								</tr>							</table>						</td>					</tr>					<tr>						<td>							<?php								$con = mysql_connect("localhost","firecoac_bnole","sadiemay");									if (!$con)										{											die('Could Not Connect: ' . mysql_error());										}									mysql_select_db("****_****", $con);									$commentNumber = 1;									$result = mysql_query("SELECT comments, signature FROM fanthefire ORDER BY added DESC LIMIT 0, 6", $con);									while ($row = mysql_fetch_assoc($result))										{											echo '<div class="comments">';											echo '"';											echo $row['comments'];											echo '"';											echo '</div>';											echo '<div class="signature">';											echo '- ';											echo $row['signature'];											echo '</div>';											if ($commentNumber < 6)												{													echo '<br /><br />';													$commentNumber++;												}										}								mysql_close($con);							?>						</td>					</tr>					<tr>						<td>							<table align="center" cellpadding="0" cellspacing="0">								<tr>									<td align="center" width="100" />									<td align="center" width="100">										<?php											$con = mysql_connect("localhost","****","****");												if (!$con)													{														die('Could Not Connect: ' . mysql_error());													}												mysql_select_db("firecoac_firecoachtiller", $con);												$result = mysql_query("SELECT * FROM fanthefire");												$num_rows = mysql_num_rows($result);												echo '<i>There are</i> <b>';												echo $num_rows;												echo '</b> <i>comments.</i>';											mysql_close($con);										?>									</td>									<td align="center" width="100">										<a href="page2.php">											<img src="../graphics/arrownext.png" border="0" alt="Next Page" />										</a>									</td>								</tr>							</table>						</td>					</tr>				</table>			</td>		</tr>		<tr>			<td align="center">				<hr />				<br />				<b>Send In Your Comments!</b>				<br />				<br />				<form action="http://www.hostmonster.com/monstermail" enctype="multipart/form-data" method="post">					<input type="hidden" name="sendtoemail" value="webmaster@firecoachtiller.com" />					<input type="hidden" name="text" value="1" />					<input type="hidden" name="redirect" value="http://www.firecoachtiller.com" />					<input type="hidden" name="subject" value="Fan The Fire" />					Name: <input type="text" name="name" /><br /><br />					<input type="radio" name="title" value="JPC Member" /> JPC Member					       <input type="radio" name="title" value="Alumnus" /> Alumnus					       <input type="radio" name="title" value="Student" /> Student					       <input type="radio" name="title" value="Fan" /> Fan					       <input type="radio" name="title" value="Other" /> Other<br /><br />					Comments: <textarea name="comments" cols="50" rows="8"></textarea><br /><br />					<input type="submit" value="Submit" />				</form>			</td>		</tr>		<tr>			<td align="center">				<hr />				<font size="-1">					Comments? Questions? Complaints? <a href="mailto:webmaster@firecoachtiller.com">Contact Us!</a>				</font>			</td>		</tr>	</table></body></html>

Link to comment
Share on other sites

If it's the same database you only need to write the connection script and database selection script once. Just put it in the beginning of the page once and that's it.

Link to comment
Share on other sites

No, even though two PHP scripts are separated, they still work together.<?php// some script?>Something<?php// some more script?>Acts exactly like:<?php// some scriptecho "Something";// some more script?>

Link to comment
Share on other sites

Any PHP code goes inside the <?php ?> tags. When a PHP page gets executed everything that you do is available until the page is finished. You can make a database connection on one page and have that page include a bunch of other pages and all those other pages will be able to use the database connection that was established in the parent page.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...