I must read a MySql Table with type, vers and variant (imported from EXCEL). With three strings (eg: abc
, 6zg
, qsdf
) i must scan thru the table and find out if there is an table entry where col1 = 'abc' AND col2 = '6zg' AND col3 = 'qsdf'
. The result must be "yes there is such an entry" or "no" (boolean). How does the SELECT
look like? My code does not work at all!
$result = mysqli_query($db, "SELECT * FROM $table_name
WHERE Type LIKE '%$typ%'
AND Version LIKE '%$version%'
AND Variant LIKE '%$variant%'");
SQL
SELECT CASE WHERE COL1='abc' AND COL2='def' AND COL3='ghi' THEN 'Yes there is such an entry' ELSE 'No' END as ColumnName from your_table
$result = mysqli_query($db, "
SELECT IF ( EXISTS ( SELECT * FROM $table_name
WHERE Type LIKE '%$typ%'
AND Version LIKE '%$version%'
AND Variant LIKE '%$variant%'
)
'YES', 'NO')
";
This gives 1 row of output, regardless of how many rows satisfy the criteria. Furthermore, it stops when if finds the first match on all 3 criteria.
The following code works:
$row = $mysqli->query("SELECT * FROM $table WHERE Typ LIKE '$typ' AND Vers LIKE '$vers' AND Var LIKE '$var'");
$result = $row->fetch_assoc();
if ($result == NULL) {
echo "Not found"; }
else {
echo "Found!"; }
</div>