I am struck while fetching data from MySQL table. I am using a conditional WHERE clause to filter out results. I have two tables whose schemas are:
CREATE TABLE IF NOT EXISTS `images` (
`image_id` int(11) unsigned NOT NULL,
`keyword_id` int(11) unsigned NOT NULL,
`url` varchar(2083) NOT NULL,
`size` varchar(50) NOT NULL,
`color` varchar(50) NOT NULL,
`type` varchar(50) NOT NULL,
`created_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=901 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `keywords` (
`id` int(11) unsigned NOT NULL,
`description` varchar(100) NOT NULL,
`created_at` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
I have saved some keywords and image links. To normalize data, I have saved image links in a separate 'images' table where 'keyword_id' is the foreign key that corresponds to 'description' (i.e. keywords) field in keywords table. With the following SQL syntax I have tried to specify a selection criteria to pull image links by keyword like "Cholesterol levels." Now, when the selection criteria fails, the script must output some errors, which is not happening with my If conditional statement I have written, and I cannot figure out why? This is my first concern.
Secondly, I would like to fetch images by not an exact keywords match. For example, if my keywords table contain a keyword "Cholesterol levels," my
script can successfully fetch images by that keyword; however, fails to execute when I search only by "Cholesterol" and/or "levels". I know Mysql's LIKE
operator, but I am not sure if it can be applied here.
$dbdriver = "mysqli";
$conn = ADONewConnection($dbdriver);
$conn->Connect($server, $user, $password, $database);
$description = "cholesterol levels";
$brecordSet = $conn->Execute('
SELECT * FROM keywords
INNER JOIN images
WHERE description = ? AND keywords.id = images.keyword_id
ORDER BY RAND()
LIMIT 1
', $description);
if (!$brecordSet)
{
print $conn->ErrorMsg();
// Optionally
print "Echo on failure";
}
else
{
$url = $brecordSet->fields['url'];
}
echo $url;
LIKEis exactly what should be applied here. Other than that, you have deviated from is considered standard MySQLi syntax which is going to lead to many other problems here.->EOFon a boolean?description = ?$conn->Execute(), likearray($description).