Wordpress: Check data by using get_results - sql

I have problem to check data if its found or not, Bellow i have table in Wordpress called (postmeta), and store data like :
meta_id : 27327
post_id : 4876
meta_key : rtb
meta_value : a:20:{s:5:"party";i:1;s:5:"email";s:18:"test#gmail.com";s:5:"phone";s:10:"05921655517";s:8:"catofres";s:6:"single";s:9:"tabletype";s:8:"openarea";s:14:"validationcode";i:8407704;s:11:"frindsname1";s:0:"";s:13:"frindsmobile1";s:0:"";s:11:"frindsname2";s:0:"";s:13:"frindsmobile2";s:0:"";s:11:"frindsname3";s:0:"";s:13:"frindsmobile3";s:0:"";s:11:"frindsname4";s:0:"";s:13:"frindsmobile4";s:0:"";s:11:"frindsname5";s:0:"";s:13:"frindsmobile5";s:0:"";s:11:"frindsname6";s:0:"";s:13:"frindsmobile6";s:0:"";s:15:"date_submission";i:1525176468;s:2:"ip";s:10:"37.8.85.11";}
Now the problem how can do SQL query that check if the data (test#gmail.com , 8407704) found it this table or not ,,,
I try this query :
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = 'rtb' ", OBJECT );
But I could not complete the check in the Query

global $wpdb;
$metas = $wpdb->get_results(
$wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta where meta_key = %s", 'rtb')
);
print_r($metas);
try also
if ( metadata_exists( 'post', $post_id, 'rtb' ) ) {
$meta_value = get_post_meta( $post_id, 'rtb', true );
}

Related

Query giving double result instead of single

I have two tables: products and current_product_attribute_values
I have tried a join query to filter them as per attribute selected by the user but when I try this with an additional condition it gives me 2 results instead of one it is including the first one which is not matching as per query:
select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?
here is my laravel Controller code :
$all = Input::all();
$q = Input::get('search_text');
$att_val = Input::get('attribute_value');
$subcat = Input::get('subcat_id');
$subcat_name = DB::table('subcategories')->where('id', $subcat)->value('subcategory_name');
$brandname = DB::table('brands')->where('subcat_id', $subcat)->value('brand_name');
$brand_id = DB::table('brands')->where('subcat_id', $subcat)->value('id');
$product_count = DB::table('products')->where('brand_id', $brand_id)->count();
if ($q != "") {
// getting multiple same name params
$query = DB::table('products');
$query->join('current_product_attribute_values', 'products.id', '=', 'current_product_attribute_values.product_id');
$j = 0;
foreach ($all as $key => $values) {
//echo 'my current get key is : ' . urldecode($key). '<br>';
if ($key == $name[$j]) {
$query->where('current_product_attribute_values.attribute_id', '=', $att_id_value[$j]);
echo'<br>';
print_r($query->toSql());
echo'<br>';
//echo '<br> key matched and have some value : <br>';
//echo count($values);
if (count($values) >= 1) {
//echo '<br> it has array inside <br>';
foreach ($values as $val) {
// or waali query in same attribute
echo'<br>';
$query->orwhere('current_product_attribute_values.attribute_value_id', '=', $val);
print_r($query->toSql());
echo'<br>';
}
}
$j++;
}
}
$records = $query->toSql();
$query->where('product_name', 'LIKE', '%' . $q . '%');
$records = $query->toSql();
print_r($records);
$products = $query->paginate(10)->setPath('');
$pagination = $products->appends(array(
'q' => Input::get('q')
));
if (count($products) > 0) {
$filters = DB::table('product_attributes')->where('subcategory_id', $subcat)->get(['attribute_title']);
} else {
$filters = array();
}
$categories = categories::where('add_to_menu', 1)->with('subcategories')->with('brands')->get();
$categoryhome = categories::where('add_to_menu', 1)->with('subcategories')->get();
return view('searchfilter')
->with('productsdata', $products)
->with('filtersdata', $filters)
->with('categories', $categories)
->with('categorieshome', $categoryhome)
->with('subcat_name', $subcat_name)
->with('subcat_id', $subcat)
->with('brandname', $brandname)
->with('product_count', $product_count)
->with('querytext', $q);
}
return 'No Details found. Try to search again !';
its easier if you use raw sql as calling db select function. ex:
$query=DB::select("select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?
");
indeed you can concat vars in raw sql if you need to, ex:
$queryBrands = "select id from brands where subcat_id =".$subcat;
//echo $queryBrands
$queryBrands = DB::select($queryBrands);
By looking at your tables, product table with id value 17 has two records in table current_product_attribute_values in column product_id (I assume this column is used as foreign key to product table).
With select *, you select all of the columns from both tables. So it would most likely cause your query to return multiple records.
My suggestions:
Only select the columns you need. Avoid using select * in the long run, i.e. select product.id, product.description, current_product_attribute_values.attribute_values ......
Make use of GROUP BY
Hope these helps.

Retrieve a single columnSQLl from database

Which code do I need to retrieve a single value i from a column from table in SQL?
user_comment_count
This is column name in table
table is :
zmar_hreviews_list_total
This is code I use with error:
<?php
$insert1 ="/// ";
$string = "ars <pre>{$insert1}</pre>";
$query = 'SELECT user_comment_count FROM zmar_hreviews_list_total WHERE contentid = '.$item->getId();
$db->setQuery( $query );
$result = $db->loadResult();
if($result) {
$result = str_replace('*','',$result);
print_r($insert1); print_r($result);
}
?>
try:
select user_comment_count from zmar_hreviews_list_total

Stumped on this little SQL query

This SQL query is currently returning all values with the word anywhere in it. For instance, typing Mon will return Armoring, Behemoth, Emotion, etc etc. I'd like it to return words that start with the value, like, Monkey, Moment, Monday, etc.
Hope my explanation makes sense, code is below.
function action_ajax_drug_suggest() {
global $wpdb;
if ( empty( $_GET['term'] ) ) {
die();
}
$drug_name = $wpdb->esc_like( $_GET['term'] );
$table = $wpdb->prefix . self::TABLE_DRUG;
$posts = $wpdb->get_results( $wpdb->prepare( "
SELECT name
FROM {$table}
WHERE name LIKE '%%%s%%'
AND details <> ''
LIMIT 5",
$drug_name ) );
foreach( $posts as $post ) {
$names[] = array(
'label' => $post->name,
);
}
wp_send_json( $names );
}
try leaving out the first two %% signs
LIKE '%%%s%%' becomes LIKE '%s%%'

take value after execute query

Im new to Codeigniter and its developing. i have tested a query succefully. But when i try to take voteid value im getting errors please help me.
$query=$this->db->query("SELECT voteid FROM Questions WHERE questionid = '$questionid'");
echo $query->voteid; //im getting errors here.
Var_dump value of query
object(CI_DB_mysql_result)#18 (8) {
["conn_id"]=>
resource(30) of type (mysql link persistent)
["result_id"]=>
resource(39) of type (mysql result)
["result_array"]=>
array(0) {
}
["result_object"]=>
array(0) {
}
["custom_result_object"]=>
array(0) {
}
["current_row"]=>
int(0)
["num_rows"]=>
int(1)
["row_data"]=>
NULL
Error
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$voteid
Filename: models/questions_model.php
$query=$this->db->query("SELECT * FROM Questions WHERE questionid = '$id'");
echo $query->row()->voteid;
try this it should work
Your query returns nothing because it's incorrect. Try: "SELECT voteid FROM Questions WHERE questionid = " . $questionid (I assume that $questionid is a variable of integer type)
Try this type of writing your queries:
$this->db->select( 'voteid' );
$this->db->from ( 'Questions' );
$this->db->where ( 'questionid', $questionid );
$result = $this->db->get( );
if ( $result->num_rows( ) > 0 )
{
$row = $result->row( );
$row->voteid;
}
Try this:
$query=$this->db->query( "SELECT voteid FROM Questions WHERE questionid = $questionid" );
$row = $query->row();
return $row->voteid;
Using '$questionid' means that your variable is a string, but in this case it's an integer.

sprintf and php

The following function gives all the related data except artist_id, when run I have checked all elements in the database and ok.
If I change the artist_id to an actual 'id' it shows in the result of the function as WHERE artist_id = 4 AND........
Confess I do not understand what is causing this.
Result of the function:
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE artist_id = AND member_id = 1 AND
image_album_id = 160
<?php
function get_data_nxtprv($fields, $where) {
$return = FALSE;
// Template
$template = "SELECT %s "
. "FROM album_images "
. "WHERE artist_id = " . $artist_id. "
AND member_id = ".$_SESSION['member_id']." %s";
// Current record
$sql = sprintf($template, $fields, $where);
$query = mysql_query($sql);
$query_result = mysql_fetch_assoc($query);
//print_r($sql);
// If data has been found
if ($query_result)
{
$return = $query_result;
}
return $return;
?>
I am not entirely sure I understand your question. But I noticed that your function uses three input variables:
$artist_id, $fields, $where
But $artist_id is not getting passed as an argument.
You would need to modify the function call:
function get_data_nxtprv($artist_id, $fields, $where)
There is an error in your SQL
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE artist_id = AND member_id = 1 AND
image_album_id = 160
should it not be
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE member_id = 1 AND
image_album_id = 160
if artist_id is one of the fields you're looking for?