0

I have a PHP document opening a connection to a database. Everything works fine - but as soon as I add one additional mysql_query request to the document, the mySQL server responds with a 500 internal server error.

Any idea why? Is there a reason that I cannot have multiple mysql_query requests in the one document?

thanks very much!

The offending PHP code is here (most of the echos are there for debugging reasons)

<?php
$q=$_GET["q"];

$con = mysql_connect('address.com', 'database', 'password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sql01_5789willgil", $con);

$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;


$sql2="SELECT * FROM place WHERE title = '".$q."'";

$result2 = mysql_query($sql2);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result2))
  {
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "<td>" . $row['latitude'] . "</td>";
  echo "<td>" . $row['longitude'] . "</td>";
  echo "<td>" . $row['image'] . "</td>";
  echo "</tr>";
  }
echo "</table>";


echo "ID equals " . $id;
echo "row equals " . $row;

mysql_close($con);
?>
Will Gill
  • 577
  • 2
  • 10
  • 21
  • 2
    The *MySQL* server responds with a 500? – Ignacio Vazquez-Abrams Dec 18 '10 at 14:49
  • 2
    Have you checked the error logs to see what exactly is happening? The 500 error internal server error comes from Apache, not mySQL so the error logs there should tell you what is going wrong. – judda Dec 18 '10 at 14:51
  • ahh, ok, I guess I misunderstood where the error was coming from. I see the error in Chrome dev tools: the message is: Failed to load resource: the server responded with a status of 500 (Internal Server Error). I assumed it was the sql server since adding the extra mysql_query caused it to break... – Will Gill Dec 18 '10 at 15:05
  • Unfortunately, my web space is hosted by a hosting company and I don't have access to the logs... :( – Will Gill Dec 18 '10 at 15:06
  • Can you post your code, my guess is that you have a syntax error and php is set up to give an internal server error when that happens. – judda Dec 18 '10 at 15:07
  • Code added to post. If I remove the line $result1 = mysql_query($sql1);, everything works fine. Put it back in: error... – Will Gill Dec 18 '10 at 15:11

2 Answers2

1

From this here:

$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;

You're adding '1' to an array. I tried this script my box:

<?php
$row = array("my", "name", "is", "foo");
$id = $row + 1;
?>

and received this error:

Fatal error: Unsupported operand types

Perhaps this is the issue the script is complaining about.

From this answer here: Apache Fall Back When PHP Fails it very well be why it's returning a 500

Community
  • 1
  • 1
Derek Downey
  • 1,512
  • 2
  • 10
  • 16
1
$sql1="SELECT * FROM place WHERE title = '".
mysql_real_escape_string($q, $con)."'"; <-- please escape

$result1 = mysql_query($sql1, $con);    <-- always specify link identifier

if ($result1)
{
  $row = mysql_fetch_array($result1);  <-- always check result exist before fetch
}
ajreal
  • 46,720
  • 11
  • 89
  • 119