very rookie code problems[Solved]

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
User avatar
nexus6
Forum Newbie
Posts: 20
Joined: Fri Jun 17, 2005 3:50 am
Location: Denmark

very rookie code problems[Solved]

Post 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?
Last edited by nexus6 on Wed Jun 22, 2005 4:02 am, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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";
User avatar
nexus6
Forum Newbie
Posts: 20
Joined: Fri Jun 17, 2005 3:50 am
Location: Denmark

Post by nexus6 »

I see :) hehehe - thats why it didnt work! very embaresing
thank you for your help!
Post Reply