I need to match a String in MySql query in Laravel - php

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);
}

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");

How to use the RETURNING update query into the DQL

I'm trying to run these following simple query (it works in postgresql)
update ja_clients set ref_inc_num = ref_inc_num + 1 where id = 43933 returning ref_inc_num;
into DQL
$query = $this->entityManager->createQuery();
$query->setDQL("UPDATE Geoop\Core\Entity\Account a SET a.jobReferenceNumber__ = a.jobReferenceNumber__ + 1 WHERE a.id = :accountId RETURNING a.jobReferenceNumber__");
$query->setParameter('accountId', $account->getId());
$total = $query->getSingleScalarResult();
but it doesn't work retuning this error:
#30 /var/www/geoop_api/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php (line 861): Doctrine\ORM\Query\Parser->syntaxError(end of string)
#31 /var/www/geoop_api/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php (line 448): Doctrine\ORM\Query\QueryException::dqlError(UPDATE Geoop\Core\Entity\Account a SET a.jobReferenceNumber__ = a.jobReferenceNumber__ + 1 WHERE a.id = :accountId RETURNING a.id as id)
DQL does not support the RETURNING clause, so you need to use native SQL. You will also have to create a result set mapping.
NOTE: I don't know the actual names of your table and columns, but assuming they are named the same as your entity and properties, this will work:
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addScalarResult('jobReferenceNumber', 'jobReferenceNumber');
$sql = "
UPDATE account a
SET a.jobReferenceNumber = a.jobReferenceNumber + 1
WHERE a.id = :accountId
RETURNING a.jobReferenceNumber
";
$query = $this->entityManager->createNativeQuery($sql, $rsm);
$query->setParameter('accountId', $account->getId());
$total = $query->getSingleScalarResult();
You may need to correct the table and column names in my example. When using native SQL, you have to use the actual names in the database, not the names of entity and properties. Also, remember that postgres converts all object names to upper case unless they are quoted.
thanks for the insight :) this is working now
Heres my updated code :
Above the class i use the ResultSetMapping
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping();
$rsm->addScalarResult('ref_inc_num', 'ref_inc_num');
$sql = "
UPDATE ja_clients
SET ref_inc_num = ref_inc_num + 1
WHERE id = :clientId
RETURNING ref_inc_num
";
$query = $this->entityManager->createNativeQuery($sql, $rsm);
$query->setParameter('clientId', $account->getId());
$refNum = $query->getSingleScalarResult();

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

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 ');

Select data from database with prefix and key with multiple where condition

hello there I have PHP query that Fetch Records from a database with where condition I have four pages in WebSite I want to Fetch data from DataBase with where Condition Ever pages Fetch own Content From database Through link It's my query that's not work
Prefix already in config file
$prefix = 'v1_';
$link = $p['link'] ;
$data_query = $db->query("
SELECT * FROM ".$prefix.$key."
WHERE "$p['link'] = $link "
");
In WebSite pages are Define with key $x
$x = 'about';
$pages[$x]['title'] = 'About Us';
$pages[$x]['link'] = 'about_us';
$pages[$x]['source'] = 0;
and same variable and own info in both pages
$x = 'objects';
$x = 'services';
and I'm using this query in website its work fine but now I want to change that I mention above details
$data_query = $db->query("
SELECT * FROM ".$prefix.$key."
WHERE online = 1 ;
");
Any contributors to this post, I thank you for your help :)
Try this:
$data_query = $db->query("SELECT * FROM {$table} WHERE link = {$link}");
you can use LIKE query
eg.
SELECT * <TABLE_NAME> WHERE COLUMN_NAME LIKE '<prefix>%'
For multiple where condition read this answere

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