-2

This query matches correctly what im after:

$sql = $pdo->query('SELECT ssn FROM doge WHERE first_name = "Anne-Christine"');

however when i write it like this, it gives me "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Anne' in 'where clause'":

$namn = "Anne-Christine";
$sql = $pdo->query('SELECT ssn FROM doge WHERE first_name = '. $namn .'');

this query gives me no result with no failures being printed:

$namn = "Anne-Christine";
$sql = $pdo->query('SELECT ssn FROM doge WHERE first_name = ". $namn ."');

same with this query:

$sql = $pdo->prepare('SELECT ssn FROM doge WHERE :first_name = first_name');
            $query->execute(array(
            ':first_name' => $namn
            ));

the last two queries also crashes the page and dont write out any more code. ive asked 3 of my friends and none of them can find the fault, nor can i. what am i doing wrong?

  • 1
    (Putting a comment because the answer from @Dillonsmart will sort you out). The reason your various queries are not working are many. Query 1 works because it is correctly formatted. Query 2 fails because you're not enclosing your criteria (Anne-Christine) in quote marks so MySQL is taking those values to mean table names (Anne and Christine). Query 3 fails because you are not passing the variable value, just the name. Query 4 fails because you haven't understood the relevant manual pages for prepared statements in the PDO docs. – DaveyBoy Sep 22 '15 at 13:54

2 Answers2

0

Give the following a go.

$sql = $db->prepare("SELECT ssn FROM doge WHERE first_name = :first_name");
$sql->execute(array(':first_name' =>$namn));
$row = $sql->fetch();
0

You don't need need add simple quotes in valeus at query just use prepared statements

$sql = $pdo->prepare('SELECT ssn FROM doge WHERE first_name = ?');
$sql->execute(array($namn));
rray
  • 2,518
  • 1
  • 28
  • 38