Reputation: 2657
I am trying to query a database and return only two results or one.
Here is my situation. I have a compound that needs to be split, then the array needs to be run through a database and it should only match 2 or 1 things in the database i.e.
I have NaHCO3 to split. The query should match only Na and HCO3. In the database i have: Na, H, C, O, CO3 & HCO3.
How do i do this? Here is my lagging code.
<?php
$compound ="NaHCO3";
$arr1 = str_split($compound);
SELECT name, formula
FROM elements, ions
WHERE name= $arr1['*']
AND formula=$arr1['*']
RETURN 2;
?>
Upvotes: 1
Views: 99
Reputation: 15464
You can split compound and again implode it with ,
as glue and later use IN
query
May be some thing like this .. I am not sure..
<?
$compound ="NaHCO3";
$arr1 = str_split($compound);
foreach($arr1 as $k=>$v){
$arr2[]="'".$v."'";
}
$str=implode(",",$arr2);
$sql="SELECT name, formula
FROM elements, ions
WHERE name IN (".$str.")";
echo $sql;
?>
output
SELECT name, formula FROM elements, ions WHERE name IN ('N','a','H','C','O','3')
Upvotes: 2