Page 1 of 1

very rookie code problems[Solved]

Posted: Tue Jun 21, 2005 8:29 pm
by nexus6
I have a database "selection.db" with 3 tables "beer", "drinks" & "blends".
what i would like is to select a table lets say "drinks" get the column of the item
lets say "screwdriver" via it's id.

in my index.php you can select the tables, and double click on the item and it directs
to my show.php

this is my show.php.

Code: Select all

$db     = "c:/server/sqlite/selection.db";
  $handle = sqlite_open($db) or die("Could not open database");

if (isset($_GET["tableName"])) {
				$table = sqlite_escape_string($_GET["tableName"]);
				$query = "SELECT * FROM $table";
				
        if (isset($_GET['id'])) {
		    $id = sqlite_escape_string($_GET['id']);
        
        $query = "SELECT * FROM $query WHERE item_id='$id' ORDER BY item_id";
				
		    $result	= sqlite_query($handle, $query) or die("Error in query: ".sqlite_error_string(sqlite_last_error($handle)));


$class = "";
   if ($count % 2 == 0) {
      $class = "row index" .$count. " even";
    } else {
      $class = "row index" .$count. " odd";
    }
       echo "<TR id=\"$row[0]\" class=\"$class\">\n";
         echo "\t<TD class=\"first\">$count</TD>\n";
            for ( $i = 0; $i <= sqlite_num_fields($result)-1; $i += 1) {
             echo "\t<TD class=\"cell\">$row[$i]</TD>\n";
         }
          echo "</TR>\n";
       $count++;
      }
    }
    sqlite_close($handle);
i get the sql error:

Error: [2 at line 19] sqlite_query() [function.sqlite-query]: near "SELECT": syntax error
Error in query: SQL logic error or missing database

how can i define my select statement so it works?

Posted: Tue Jun 21, 2005 9:33 pm
by Burrito
you're using this:

Code: Select all

$query = &quote;SELECT * FROM $query WHERE item_id='$id' ORDER BY item_id&quote;;
where $query is already:

Code: Select all

"SELECT * FROM $table"
which will make the final product:

Code: Select all

SELECT * FROM SELECT * FROM $table WHERE item_id='$id' ORDER BY item_id
obviously that wont' fly.

get rid of your first $query declaration and just change the bottom one to this:

Code: Select all

$query = "SELECT * FROM $table WHERE item_id='$id' ORDER BY item_id";

Posted: Wed Jun 22, 2005 3:55 am
by nexus6
I see :) hehehe - thats why it didnt work! very embaresing
thank you for your help!