Pluto
Pluto

Reputation: 333

select a single value though a table in wordpress

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

Answers (1)

Ryan B
Ryan B

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

Related Questions