php - Getting right value from $wpdb -
before creating new post programmatically want check if there post same headline in database.
i checking this:
$results = $wpdb->get_results("select count(*) $wpdb->posts post_type = 'candidates' , post_title = '$name'");
example of value when var_dump($results):
array(1) { [0]=> object(stdclass)#85 (1) { ["count(*)"]=> string(1) "8" } }
now want string(1) "8"
, add variable , convert number, not sure how it.
i tried
$number = (int)$results[0]->count(*)
but isn't right. tried other combinations couldn't figure out
your problem (*)
in $number = (int)$results[0]->count(*)
. cannot use these special characters 'variable'. you're better off using column alias using as your_column_alias
.
so want use
select count(*) post_count $wpdb->posts post_type = 'candidates' , post_title = '$name'
as query ,
$number = (int) $results[0]->post_count;
to fetch aliased column resultset.
Comments
Post a Comment