Reputation: 333
The table has these columns:category_id
, category_name
, and category_parent_name
I would like to get category_id
by using category_name
. I searched online, and the code is like below:
$ad_cat
is the category name.
global $wpdb;
$tbl_categories = $wpdb->prefix."awpcp_categories";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $tbl_categories where category_name =".$ad_cat );
$category_id=intval($retrieve_data->category_id);
I echo'ed category name and id
for testing. The name shows fine, but id
is always 0
.
Any idea or solution?
Upvotes: 3
Views: 3959
Reputation: 3392
It says the error because your query isn't correct. Try:
$retrieve_data = $wpdb->get_var( "SELECT category_name FROM $tbl_categories
where category_name ='".$ad_cat."'" )
Upvotes: 2