1

Possible Duplicate:
Reference - What does this error mean in PHP?

Good Day,

I have a html form that has an input box, a submit button and a result box. On submit, the input is sent to the php file where a query is done and I'm trying to echo back a field from the database back to the result box, which I've called 'answer' in my form.

Here is my php file:

<?php
$hostname = 'myhost.com';
$username = 'ratetable';
$password = 'mypassword';
$term = (int) $_GET['term'];

try {
   $dbh = new PDO("mysql:host=$hostname;dbname=ratetable", $username, $password);
   echo 'Connected to database<br />';

   foreach($db->query('SELECT * FROM rates WHERE mileage<=$term' DESC LIMIT 1) as $row) {
      echo "<input type='text' name='answer' value='$row['ratepermile']'>";
   }
}         

?>

However, I'm getting "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" on the line where I want to echo a field (ratepermile) from the row in the database back to the 'answer' text box. I did some checking and found that this error means that I'm missing some closing bracket or something like that but I'm not seeing what it's choking on.

Can someone please tell me what is creating the problem?

Thanks for having a look.

Community
  • 1
  • 1
Shawn
  • 147
  • 1
  • 4
  • 16

2 Answers2

2

You have mismatched quotes

foreach($db->query('SELECT * FROM rates WHERE mileage<= ' . $term . ' DESC LIMIT 1') as $row) {

And here

echo "<input type='text' name='answer' value='" . $row['ratepermile'] . "'>";

You also have no catch clause.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • Hi Mike, I am now getting "Fatal error: Call to a member function query() on a non-object" on my line that now reads: foreach($db->query('SELECT * FROM rates WHERE mileage<= ' . $term . ' ORDER BY mileage DESC LIMIT 1') as $row) { - I don't understand what the non-object is, however. – Shawn Jan 16 '13 at 16:04
  • @Shawn That's nowhere in the code you posted.. and I'm not going to debug everything for you. People have given you the tools you need to understand the error and how to fix it. It's up to you now. – Mike B Jan 16 '13 at 16:26
0

Replace this :

foreach($db->query("SELECT * FROM rates WHERE mileage<='".$term."' DESC LIMIT 1") as $row) {
  echo "<input type='text' name='answer' value='".$row['ratepermile']."'>";
}

with your foreach code And put

catch (PDOException $e) {
  echo $e->getMessage();
  throw($e);
}

after try{ }

hope this will help you!

sandip
  • 3,279
  • 5
  • 31
  • 54