php - how to query one column by id in sql -
i wondering if figure query issue out. have googled around cannot find looking. hope can explain here.
i have products table contains, categoryid, productid, , imagefile columns.
i trying create function in can put variable images connects specific category id.
this function have far.
function get_image_by_category($category_id) { global $db; $query = 'select * products products.imagefile = :category_id'; $statement = $db->prepare($query); $statement->bindvalue(":category_id", $category_id); $statement->execute(); $category_image = $statement->fetchall(); $statement->closecursor(); return $category_image; }
i m using var_dump , comes null , array size 0;
this code use function in index image.
if ($action == 'list_products') { $category_id = filter_input(input_get, 'category_id', filter_validate_int); if ($category_id == null || $category_id == false) { $category_id = 1; } $categories = get_categories(); $category_image = get_image_by_category($category_id); var_dump($category_image); }
please let me know if need more info.
thanks!
your query says this:
select * products products.imagefile = :category_id
it doesn't make sense compare image file name category id. never come out equal.
also, you're using select *
. pro tip: don't in software; instead name columns want returned.
try query instead:
select imagefile products category_id = :category_id
if have lots of images each category , want them in single string comma separated, try this.
select group_concat(imagefile) images products category_id = :category_id
Comments
Post a Comment