0
    <?php

    // Working SELECT query.
    $db = new SQLite3('casino.db');

    // This works great and returns only name = bergpau !!!
    $results = $db->query('SELECT * FROM employe WHERE username="bergpau"');
   while ($row = $results->fetchArray()) 
         print  $row['nom'] . "<BR>";

   // This DOES NOT WORK AT ALL, returns no values !!! HELP !!!
   $astring = "bergpau";
   $results = $db->query('SELECT * FROM employe WHERE username=$astring');
   while ($row = $results->fetchArray()) 
        print  $row['nom'] . "<BR>";

   ?>

Opening database, ok return no value since it cannot validate string in WHERE clause

2
  • Did you check for errors? The second SELECT looks like it would result in a mysql error Commented Jul 8, 2016 at 2:29
  • stackoverflow.com/questions/5605965/… this will be of interest to you Commented Jul 8, 2016 at 2:31

1 Answer 1

1

Strings, in SQL (and PHP), need to be quoted like you did in your first query (username="bergpau"). This also could open you to SQL injections; you should use parameterized queries.

$results = $db->query("SELECT * FROM employe WHERE username='{$astring}'");

Also variables in single quotes aren't processed by PHP.

Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

-http://php.net/manual/en/language.types.string.php

Alternatively you could pass the quotes in the assignment (note the double quotes are for the encapsulation in PHP, the single quotes are stored in the value of $astring):

$astring = "'bergpau'";

then you'd just need to concatenate the variable.

$results = $db->query('SELECT * FROM employe WHERE username=' . $astring);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.