Changing a text to variable for where clause in PDO query

$stmt = $dbc->prepare('SELECT id, myNumeric FROM myTable WHERE myString = ?'); 

$stmt->execute(['aaaa']);
$row = $stmt->fetch();  
echo '(' .$row['id']. ') ' .$row['myNumeric'];

The code above produces the result below

I like to change aaaa in “$stmt->execute([‘aaaa’]);” to a variable.
The code2 below is one of my trials for it.

$stmt = $dbc->prepare('SELECT id, myNumeric FROM myTable WHERE myString = ?'); 

$myString="aaaa";
$stmt->execute(['$myString']);
$row = $stmt->fetch();  
echo '(' .$row['id']. ') ' .$row['myNumeric'];

But the result2 is the following.

Why have you put the variable name in quotes in your execute() call? It might possibly work if you used double-quotes instead of single ones, but there’s no need for either in that position. What you’re specifying, by putting quotes around it, is a string that contains $myString instead of a string that contains aaaa.

1 Like

The code above without any quotation marks works fine. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.