Spaceman Spaceman - 5 months ago 10
SQL Question

Php Select Statement works with id as record selector but will not work if I use a different column as a selector

Php Select Statement works with id(with unique values) as record selector but will not work if I use a different column(with unique values) as a selector

THIS WORKS

$Idart = "4";
$sql2 = "SELECT * FROM articles where id in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();


THIS DOES NOT WORK

$Idart = "5-6142-8906-6641";
$sql2 = "SELECT * FROM articles where IDStamp in ({$Idart})";
$results2 = $conn->query($sql2);
$row2 = $results2->fetch_assoc();


I've tried a variety of different things with MYSQL including deleting "id" column and making "IDStamp" the primary key. Any thoughts appreciated.

Answer

Because You are putting string in IN statement, without quotes.

$sql2 = "SELECT * FROM articles where IDStamp in ('{$Idart}')";

Should work. However, You should look into prepare statements