0

I have an sqlite database of mtcars data:

> mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
...

I have following html file which produces a form all right:

<html>
<form action="mtcars_action.php" method="post">
 <p>Enter cyl (4,6,8): <input type="number" name="cyl" /></p>
 <p>Enter gear (3,4,5): <input type="number" name="gear" /></p>
 <p>Enter carb (1,2,3,4,6,8): <input type="number" name="carb" /></p>
 <p><input type="submit"/></p>
</form>
</html>

I am trying to read mtcars.sqlite database and return output with following php code file (mtcars_action.php):

<?php

echo "<p>starting</p>" or die ("error 0"); 

$db = new SQLite3('/var/databases/mtcars.sqlite') or die("error 1") ;

$results = $db->query("select * from main where cyl=$_POST['cyl'] & gear=$_POST['gear'] & carb=$_POST['carb'] ;") or die("error 2");

while ($row = $results->fetchArray()) {
    echo "model: $row[model]; ";
}

?>

This is not working. No output comes. Even 'starting' (from first print statement) does not come.

Keeping only one statement in the file works:

print($_POST['cyl'])

or 'gear' or 'carb' works all right, indicating that the numbers are being received all right.

Adding "$results = $db->query...." statement stops all output and I suspect the error is in this line. Removing "" does not help.

Where is the error and how can this be solved? I am using httpd (Apache) web server on Slackware Linux and testing it on localhost.

EDIT: I turned display_errors = on in php.ini file and I get this message:

Parse error: syntax error, unexpected 'main' (T_STRING) in /var/www/htdocs/mtcars_action.php on line 7 . 

But 'main' is the name of a table in this database. The command 'select * from main;' works perfectly in sqlite3. The problem persists even after changing the name of the table from "main" to "mtcars".

rnso
  • 23,686
  • 25
  • 112
  • 234
  • single ampersands are failing you, for one thing. Use 2 `&&` or `AND`. – Funk Forty Niner Jun 08 '16 at 14:58
  • http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Digital Chris Jun 08 '16 at 14:59
  • @DigitalChris their use of a single ampersand `$_POST['cyl'] & gear=$_POST['gear'] & carb=$_POST['carb']` may be seen as a valid expression, just not a "successful" one though. Therefore, error checking "might" fail them here and "silently". However, it might just help them for sure. OP hasn't responded to my earlier comment about it though. – Funk Forty Niner Jun 08 '16 at 15:04
  • I stood around this question long enough. You'll need to ping someone when you get to reading these. – Funk Forty Niner Jun 08 '16 at 15:09
  • @Fred-ii- Apologies for the delay. I tried '&&' and 'AND' but none works. – rnso Jun 08 '16 at 16:08

0 Answers0