How to use Two Where Clauses use in Php Mysql Search? - php

PHP/MYSQL:
How To use Normal Where Clause And Search Where Clause Use to Mysqli Query
$search = $_POST['cloud_edge'];
$cloud_edge = 1;
$network_countries = $wpdb->get_results('SELECT * From media_networks WHERE
cloud_edge = "'.$cloud_edge.'" AND capital_city LIKE "'%$search%'" ORDER by
capital_city ');

$network_countries = $wpdb->get_results('SELECT * From media_networks WHERE
cloud_edge = "'.$cloud_edge.'" AND capital_city LIKE "%'.$search.'%" ORDER by
capital_city ');

You are missing concatenation on second variable.
<?php
$search = $_POST['cloud_edge'];
$cloud_edge = 1;
$network_countries = $wpdb->get_results('SELECT * From media_networks WHERE
cloud_edge = "'.$cloud_edge.'" AND capital_city LIKE "%'.$search.'%" ORDER by
capital_city ');

You need to concatenate search variable properly
LIKE "%'.$search.'%"
Also you can't add % with variable it won't return the result. Try following
$network_countries = $wpdb->get_results('SELECT * From media_networks WHERE
cloud_edge = "'.$cloud_edge.'" AND capital_city LIKE "%'.$search.'%" ORDER by
capital_city ');

Related

SELECT query inside PHP class

I am trying to run a simple SELECT query inside of a PHP class, using the GET variable.
$this->Token = $_GET['Token'] ?? null;
function getRows(){
$query = $this->db->query("SELECT * FROM store_product_images WHERE token = ".$this->Token." ORDER BY display_order ASC");
When I run this, nothing shows, if I remove the WHERE it works fine
you missed the quote
$query = $this->db->query("SELECT * FROM store_product_images WHERE `token`= '".$this->Token."' ORDER BY display_order ASC");

I need to match a String in MySql query in Laravel

I need to use a query which perfectly works in MySql. and query is
SELECT * FROM c2cxdb.jobs where job_title like '%Java%' and job_policy = 'Public' and job_status = 1;
where I need to replace java inside %% with a dynamic value that I pass to the method.
function AllJobs($job_title){
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like '%$job_title%' and job_policy = "Public" and job_status = 1');
dd($allJobs);
}
But It shows Division by zero exception when I run it.How to replace the value in my query with the value I Pass.I need to get the $job_title value into query.
function AllJobs($job_title){
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like "%'.$job_title.'%" and job_policy = "Public" and job_status = 1');
dd($allJobs);
}
function AllJobs($job_title){
$job_title="%".$job_title."%";
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like "'.$job_title.'" and job_policy = "Public" and job_status = 1');
dd($allJobs);
}

LIKE command in Mysql

My query:
$sel = mysql_query("select * from categorydetails where productname LIKE '%$commonsearch%' ");
Where product name = tvs apache rtr:
commonsearch = rtr
but it's not working
is there a way to use like command when mysql column contain more than one word?
$sel = mysql_query("select * from categorydetails where productname LIKE '%___rtr%' ");
see the example
SELECT * FROM certificates where csr REGEXP 'tvs'
your query
$sel = mysql_query("select * from categorydetails where productname REGEXP '$commonsearch' ");
Please use below query this is working on single word and multipe string like "test is test"
$sel = mysql_query("select * from categorydetails where productname LIKE '%rtr%' or productname LIKE '%rtr' or productname LIKE 'rtr%' ");
Use this
LIKE '%".$commonsearch."%

$wpdb->wp_users returning empty value for

I have a code Example:
function custom_func(){
global $wpdb;
$wpdb->flush(); //tried with and without this line
$getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';
$arrayReturned = $wpdb->get_results($wpdb->prepare($getTest));
}
From what I've read I thought that $wpdb->wp_users is meant to have returned the database name and table name like so dbName.tableName; but it just returns an empty value.
I've tried:
$getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';
which shows as the following to wordpress:
SELECT * FROM $wpdb->wp_users LIMIT 1
and
$getTest = 'SELECT * FROM '.$wpdb->wp_users.' LIMIT 1';
which shows as the following to wordpress:
SELECT * FROM LIMIT 1
I can't fathom why this isn't working since this is all based on literature from the wordpress codex, any thoughts?
You don't need to prefix the tables. As $wpdb->table will do it for you. Also you need to use double quotes " instead of single ' because you are using $wpdb variable in your query string.
'SELECT * FROM $wpdb->wp_users LIMIT 1';
^---------------------^^^-------------^---
Use it without table prefix and with double quotes ".
"SELECT * FROM $wpdb->users LIMIT 1";
Also you don't need to use prepared statement because as there are no user input.
Your code should look like this:
function custom_func() {
global $wpdb;
$getTest = "SELECT * FROM $wpdb->users LIMIT 1";
$arrayReturned = $wpdb->get_results($getTest);
var_dump($arrayReturned); // see results
}

PHP Retrieve results

I am having a small trouble retrieving results that I hope someone can help me with.
I have a field called $incategory which is a comma based string, and what I want to do is explode the into an array that can be used to retrieve results as below (Hope that makes sense):
<?php
$showlist = $row_listelements['incategory'];
// ** e.g. $incategory = 1,3,5,
// ** What I want to do is look at table 'category'
// ** and retrieve results with an 'id' of either 1, 3 or 5
// ** Display Results
mysql_select_db($database_db, $db);
$query_display = "SELECT * FROM category WHERE id = ".$showlist." ORDER BY name ASC";
$display = mysql_query($query_display, $db) or die(mysql_error());
$row_display = mysql_fetch_assoc($display);
$totalRows_display = mysql_num_rows($display);
?>
You can use the IN keyword of SQL directly like this.
query_display = "SELECT * FROM category WHERE id IN (".$showlist.") ORDER BY name ASC";
Another tip would be to stop using MYSQL_QUERY as it is deprecated in PHP 5.3
Edit: If $showlist = '1,3,5,' you will need to remove the last comma from the string to make it useable in the query. Just use this query then
query_display = "SELECT * FROM category WHERE id IN ('".str_replace(",", "','", substr($showlist, -1))."') ORDER BY name ASC";
Use explode function and use , as delimiter.
refer here http://www.w3schools.com/php/func_string_explode.asp
Hope this helps.
First, you have explode the $incategory string into an array containing all of the category number. For example:
$incategory = explode(",", $incategory);
And then you just have to execute this query:
$query_display = "SELECT * FROM category WHERE id = "
. $incategory[$i] . " ORDER BY name ASC";
The $i should be defined beforehand (usually using loop).

Categories