How to put 2 SQL querys together? - php

I'm trying to put together 2 sql query one for name :
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'ORDER BY id DESC";
and one to get $from and $to:
$sql = "SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')";
Can you suggest how it is possible to combine these two query to show one name selected aswell and from and to for that name that is selected aswell as to just show all results for name selected without the from and to?

SELECT * FROM register WHERE time BETWEEN #from AND #to
UNION
SELECT * FROM register WHERE name LIKE #name ORDER BY id DESC

This can be done using UNION, or alternatively, you could use OR condition:
SELECT *
FROM register
WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC;

SELECT * FROM register WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC

You can use Union to combine the results of both queries
SELECT * FROM register WHERE name LIKE '" .$name ."'
union all
SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')

Your question is unclear but still can try something like this
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'
OR (time BETWEEN ('$from') AND ('$to')) ORDER BY id DESC";

This is what I was after, thanks to my Oracle programmer you know who you are, Thought I share:
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."' and time between ifnull(timestamp('$from'),time) and ifnull(timestamp('$to'),time) ORDER BY id DESC";

Related

PHP + MySQL 3 Condition

so I have this query below in my php code :
$query ="SELECT *
FROM material_tools_master_data
WHERE material_name like '" . $_POST["keyword"] . "%'
ORDER BY material_code
LIMIT 0,50";
It does pretty well and give me a result called 'autocomplete' in my form. The problem is, I wanna make it more complex, I want my autocomplete filter the data selection not only by material_name but also with material_tools_group and show me exactly the material_name which is filtered by material_group = 'Measuring' OR 'Tools'.
The point is, I want to make this query works with my autocomplete. So here is my new query :
$query ="SELECT *
FROM material_tools_master_data
WHERE `material_tools_group` = 'Measuring' OR 'Tools' AND `material_name` like '" . $_POST["keyword"] . "%'
ORDER BY material_code LIMIT 0,50";
The query above is not working, the query above is giving me all the material_name rows in the table.
Any help will be much appreciated.
See warnings about PHP's deprecated API, and the proper use of prepared statements above...
$query ="
SELECT *
FROM material_tools_master_data
WHERE material_tools_group IN('Measuring','Tools')
AND `material_name` LIKE '" . $_POST["keyword"] . "%'
ORDER
BY material_code LIMIT 0,50;
";

How to SELECT LIKE from SQL without duplicate viewing id

I am using this code :
<?
$i_count=1;
$sSQL = "SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5";
$mysql_result = mysql_query($sSQL, $GLOBALS['conn']);
$num_rows = mysql_num_rows($mysql_result);
if ($num_rows > 0) {
?>
<br />
It will show the project with same location with viewing project. But it also show the current project in the result.
Ex : I have 02 projects in location Hanoi name A and B. When I view project A, the related projects show both A and B project.
How I can fix it ?
Thanks
Exclude the current project in the WHERE:
SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' AND id <> " . $a_project[0]["id"] . " ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5
(Or something like it, depending on your field names and such)

echoing max or last greater id in a table

I want to get the maximum id from a table.
If I use mysqli_insert_id, then it gives 0 value.Plz help or suggest any other approach to get last/max id from table
$pre_id = $_POST['last_id'];
$sql = mysqli_query($db3->connection, "SELECT * FROM chat where id=>_pre_id");
$id = mysqli_insert_id($db3->connection);
echo $id;
Is that you want ?
SELECT * FROM chat ORDER BY id DESC LIMIT 0,1
try this query
SELECT max(id) as maxid FROM chat

orderTotal and orderId inside tpl_checkout_success_default - zen-cart

any ideas how to get orderTotal and orderId inside the tpl_checkout_success_default for the conversions tracking purposes ?
So far it looks like order id can be accessed by using this variable $zv_orders_id but how to get order total ?
will this code work:
$orders_query = "SELECT * FROM zen_orders WHERE orders_id = " . $zv_orders_id ." LIMIT 1";
$orders = $db->Execute($orders_query);
$order_total = $orders->fields['order_total'];
many thanks,
cheers
look in /includes/modules/pages/checkout_success/header_php.php
in there you will see the queries already being run by zencart to do with your order, and id say its already pulling out the info you want.
so you just need to set said data you need to a variable that you can then use in your tpl_checkout_success_default.php file.
eg, something like $customer_has_gv_balance, you will see where it is set in the hearder file and then used in the template file
heres something i found in order.php that would almost do it as is:
$order_total_query = "select text, value
from " . TABLE_ORDERS_TOTAL . "
where orders_id = '" . (int)$order_id . "'
and class = 'ot_total'";
$order_total = $db->Execute($order_total_query);
For a simple tracking code like one used for a shopping comparison site, I've used the following for the order ID and order amount. Use these in the tpl_checkout_success.php page
Order ID:
echo $zv_orders_id;
Use this select statement:
$to_send_sql = 'select REPLACE (text,"$","") text from orders_total where orders_id = '.$zv_orders_id.' and class = "ot_subtotal"';
$to_send= $db->Execute($to_send_sql);
Order amount:
echo $to_send->fields['text'];
Hope this helps someone!

Search within database field

I am building a job search site where candidates can register themselves for various jobs . Also employers can post jobs with multiple qualification (eg if he wants MBA or Phd it will be saved in database like mba,phd . I want to show jobs for the registered candidates which are relevant to his educational qualification . So I am confused how can I do this because I think I have to search within database . Please help.
if($cid >0 ) //If the candidate is registered
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE "; //I want to put a condition if he is registered with us , it will show jobs a/c to his qualification
}
UPDATE :
$cond = "";
if($cid >0 )
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE emp_qualification LIKE '%$quali%'";
}
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
$cond
ORDER BY job_add_date DESC LIMIT 0,10
";
// Query Search all jobs
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
ORDER BY job_add_date DESC LIMIT 0,10
";
You use the LIKE keyword - for example,
SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
WHERE emp_qualification LIKE '%MBA%'
ORDER BY job_add_date DESC LIMIT 0,10
Here, the '%' wildcard is used for any number of characters before or after MBA, so it will find it in strings like MBA, BTech, MBA, BCom, MBA, PhD.
set a colum in your tbl_emp_data names 'is_registered' type ENUM and set two value ('0','1')
and then set WHERE condition
WHERE is_registered = 1

Categories