Why is MySQL Connection Closing After ?>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coojinx
Forum Newbie
Posts: 2
Joined: Fri Oct 16, 2009 2:13 pm

Why is MySQL Connection Closing After ?>

Post by coojinx »

The first query works fine and data is returned.

The second query fails. If I reopen the mysql connection it works. Why is my connection being closed? Does the mysql connection close automatically when it hits the php closing tag? It's not a big deal, it just seems like I should be able to leave the connection to mysql open until the end of the php document.

Code: Select all

<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost", "name", "password") or die(mysql_error());
mysql_select_db("intf", $con);
 
$sql = "select o.id, ok.kind, ol.label ";
$sql .= "from objs o ";
$sql .= "inner join objkinds ok ";
$sql .= "on o.kind=ok.id ";
$sql .= "left join objlabels ol ";
$sql .= "on o.v1=ol.id ";
$sql .= "where o.loc=$loc ";
 
$result = mysql_query($sql);
 
while($row = mysql_fetch_array($result)) {
    echo "<a href='objs2.php?id=$row[id]'>$row[id] - $row[kind] - $row[label]</a><br />";
} 
 
 
?>
<p>
<form name="input" action="html_form_submit.asp" method="post">
type: <select name="sections">
<? 
$sql = "select id, kind ";
$sql .= "from objkinds ";
 
$result = mysql_query($sql);
 
 
while($row = mysql_fetch_array($result)) {
    echo "<option valye=$row[id]>$row[kind]</option>";
} 
mysql_close($con);
?>
</select>
label: <input type="text" name="label">
<input type="submit" value="Submit" />
</form>
</p>
</body>
</html>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why is MySQL Connection Closing After ?>

Post by requinix »

What error message do you get? Server has gone away?
coojinx
Forum Newbie
Posts: 2
Joined: Fri Oct 16, 2009 2:13 pm

Re: Why is MySQL Connection Closing After ?>

Post by coojinx »

the second query is just failing to execute. no error is generated. i'm assuming the mysql connection is being closed because this code works fine

Code: Select all

<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("intf", $con);
 
$sql = "select o.id, ok.kind, ol.label ";
$sql .= "from objs o ";
$sql .= "inner join objkinds ok ";
$sql .= "on o.kind=ok.id ";
$sql .= "left join objlabels ol ";
$sql .= "on o.v1=ol.id ";
$sql .= "where o.loc=$loc ";
 
$result = mysql_query($sql);
 
while($row = mysql_fetch_array($result)) {
    echo "<a href='objs2.php?id=$row[id]'>$row[id] - $row[kind] - $row[label]</a><br />";
} 
 
 
?>
<p>
<form name="input" action="obj2a.php" method="post">
type: <select name="kinds">
<? 
$con = mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("intf", $con);
 
$sql = "select id, kind ";
$sql .= "from objkinds ";
 
$result = mysql_query($sql);
 
 
while($row = mysql_fetch_array($result)) {
    echo "<option value=$row[id]>$row[kind]</option>";
} 
mysql_close($con);
?>
</select>
label: <input type="text" name="label">
<input type="hidden" name="loc" value="<?php echo $loc; ?>">
<input type="submit" value="Submit" />
</form>
</p>
</body>
</html>
i haven't used php for a long time
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why is MySQL Connection Closing After ?>

Post by Christopher »

You do no error checking. What do you get if you do this:

Code: Select all

$sql = "select id, kind ";
$sql .= "from objkinds ";
$result = mysql_query($sql);
echo mysql_error();
(#10850)
Post Reply