How to write sql query in php with if statement in WHERE - php

How do I write a SQL query with an if statement in the WHERE part of a query?
Here is my sql:
$sql = 'SELECT First, Second,
Third,Fifth, Status, Isik_id, Comments, Code
FROM Persons WHERE Status = 1 && Third = "'.$whoislogged.'" ORDER BY RAND() LIMIT 1';
The Third = "'.$whoislogged.'" is a word what comes from session.
If $whoislogged exists and is equal to Third then the query should be like:
$sql = 'SELECT First, Second, Third,Fifth, Status, Isik_id, Comments, Code
FROM Persons
WHERE Status = 1 && Third = "'.$whoislogged.'" ORDER BY RAND() LIMIT 1';
But if the $whoislogged dosent appear then the query should be like:
$sql = 'SELECT First, Second,
Third,Fifth, Status, Isik_id, Comments, Code
FROM Persons WHERE Status = 1 ORDER BY RAND() LIMIT 1';
So how can I put all this into one query?

You can handle it in query itself.
WHERE Status = 1 and if(Third = "'.$whoislogged.'", Third = "'.$whoislogged.'" , 1=1)
the condition part will check third is match with $whoislogged if it's true then add id as condition otherwise omit this condition.

Simply make a new variable that contains the Third statement
if ( !empty($whoislogged) ) $third = "AND Third = ". $whoislogged;
else $third = "";
$sql = 'SELECT First, Second,
Third,Fifth, Status, Isik_id, Comments, Code
FROM Persons WHERE Status = 1 '. $third .' ORDER BY RAND() LIMIT 1';
It is not the most efficient way but it works

Related

PHP Query - Exclude if exist in table

orderfood
orderfood_id food_id total_amount
foodcancel
foodcancel_id food_id status
$query = $this->db->query("SELECT * FROM order_food of LEFT JOIN `foodcancel` fc ON of.food_id = fc.food_id WHERE of.orderfood_id = '" . (int)$orderfood_id . "'");
$order_foods = $query->rows;
above is my query, what i wanted is that if there food_id inside foodcancel table , exclude it from rows, possbile to do it ?
For exclude the existing values you could try checking null for corresponding matching value
SELECT *
FROM order_food of
LEFT JOIN foodcancel fc ON of.food_id = fc.food_id
and of.food_id = your_value
WHERE fc.orderfood_id is null
anyway you should not php var in your sql code because in this way you are are risk for sqlinjection for avoid this you should take a look at prepared statement and binding param
It's very possible to do. In my logic. first, you must get all food_id on food_cancel table. Then save it into variabel and use it when you show orderFood table with adding NOT IN condition.
I've write code for you,
<?php
// Get Food Id From Cancel
$orderCancel = mysqli_query($mysqli, "SELECT * FROM `foodcancel`");
$cancelId = "";
while ($cancel = mysqli_fetch_array($orderCancel)) {
$cancelId .= $cancel["food_id"].",";
};
$cancelId = substr($cancelId, 0, -1);
// Put Food Id on Cancel Table into NOT IN Condition Database
$orderFood = mysqli_query($mysqli, "SELECT * FROM `orderfood` WHERE food_id NOT IN ($cancelId)");
while ($order = mysqli_fetch_assoc($orderFood)) {
$food[] = $order;
};
echo json_encode($food);
?>

Select from 2 tables not working with php mysql

I have two different tables of the following structure:
grouprel
id | userId | pupID | groupId
pupils
id | userId | fname | lname
pupId in groulrel is equal to id in pupils.
I want to fetch pupils from a different group and then order them by fname, lname.
Now I have two queries like this:
$q = "SELECT * FROM grouprel WHERE userid = ". $userid ." AND groupId = ". $_GET['id'] ."";
$r = mysqli_query($mysqli, $q);
while ($rows = mysqli_fetch_object($r)) {
$query = "SELECT id, fname, lname FROM pupils WHERE userid = ". $userid ." AND id = ". $rows->pupId ." AND status = 0 ORDER BY fname, lname";
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_object($result)) {
echo stuff...
}
}
This works, but it doesn't order the names alphabetically like I want to.
How could I fix this?
This is iterating over the first query:
while ($rows = mysqli_fetch_object($r)) {
And this iterates over each instance of the second query:
while($row = mysqli_fetch_object($result)) {
So if the first query returns 1,2,3, and each iteration of the second query returns A,B, then your output would be:
1 A
1 B
2 A
2 B
3 A
3 B
The second query is ordering by the ORDER BY clause you gave it. But you are ordering the entire output by the first query.
Ultimately, why do you need these separate queries at all? Executing a database query in a loop is almost always the wrong idea. It looks like all you need is one query with a simple JOIN. Guessing on your logic, something like this:
SELECT
pupils.id, pupils.fname, pupils.lname
FROM
pupils
INNER JOIN grouprel ON pupils.id = grouprel.pupId
WHERE
pupils.userid = ?
AND grouprel.groupId = ?
AND pupils.status = 0
ORDER BY
fname, lname
It may take a little tweaking to match exactly what you're looking for, but you can achieve your goal with a single query instead of multiple separate queries. Then the results of that query will be ordered the way you told MySQL to order them, instead of the way you told PHP to order them.

How can I select a mysql row that has a status and 2 other variables?

OK, I don't know if I asked this correctly at all, but what I want to do is select a row based on the status and the user. So this is what I have:
$sql = "SELECT * FROM w_o WHERE maint_user = '".$user_name."' AND w_status = 'active' OR w_status = 'open' ORDER BY r_date";
For some reason it shows me some entries that don't have the maint_user = $user_name. I don't know why that is. It seems to work when I assign it to a certain user, but then when I re-assign it, it just pulls up all of the rows.
Any help would be awesome!!! Thank you!!
You need to wrap part of your WHERE cause in parenthesis to enforce operator precedence
WHERE maint_user = '".$user_name."' AND (w_status = 'active' OR w_status = 'open')
Bassed of what you have shown I would add ascending or descending to you sql query. Also wrap WHERE in parentheses.
$sql = "SELECT * FROM w_o WHERE maint_user = '".$user_name."' AND (w_status = 'active' OR w_status = 'open') ORDER BY r_date DESC";

mysql - if value = 0 shorten where statement

I wonder if it's possible to shorten query depending on some variable value in elegant way.
For example: I have value named $var = 0 and I would like to send a query that looks like this:
$query = "SELECT id, name, quantity FROM products WHERE quantity > 100";
But whan the $var != 1 I'd like to send a query like this:
$query = "SELECT id, name, quantity FROM products WHERE quantity > 100 AND id = '$var'";
So depending on value of $var I want to execute one of queries. They differ only with last expression.
I found two possible solutions but they are not elegant and I dont like them at all.
One is made in php:
if ( $var == 0 ) {
$query_without_second_expression
} else {
$query_with_second_expression
}
Second is made in mysql:
SELECT WHEN '$var' <> 0 THEN id, name, quantity
FROM products WHERE quantity > 100 AND id = '$var' ELSE id, name, quantity
FROM products WHERE quantity > 100 END
but i dont like it - each idea doubles queries in some whay. Can I do something like this?
SELECT id, name, quantity
FROM products WHERE quantity > 100
CASE WHEN $var <> 0 THEN AND id = '$var'
It's much shorter, and adds part of query if needed. Of course real query is much more complicated and shorter statement would be really expected. Anyone has an idea?
If I understand well..
$query = "SELECT id, name, quantity FROM products WHERE quantity > 100";
if ( $var != 0 ) {
$query .= " AND id = '$var'";
}
do you like it?
You could so something like this on the SQL side:
"SELECT id, name, quantity FROM products WHERE quantity > 100 AND (id = '$var' OR '$var' = 0)
But performance could be impacted. I would suggest building the appropriate query on the PHP side.
I'm no PHP developer (it is PHP, right?), but wouldn't it be easiest to build your query from a concatenated string?
Pseudo-code:
$my_query = "SELECT id, name, quantity FROM products WHERE quantity > 100"
if ($var != 1)
$my_query = $my_query + " AND id = '$var'";
end if;
/*go ahead with your query*/
You can do this:
SELECT id, name, quantity
FROM products
WHERE 1 = 1
AND ( $q IS NULL OR quantity > $q)
AND ( $var IS NULL OR id = $var)
If you want only the first condition to run then pass $q = 100 and $var = NULL, therefore the second condition will be ignored. And for the second query pass the $q = 100 and $var = id value and you will got the your second query.
If they only differ in additional where-statements, I would probably still stay in PHP and do the following:
$conditions = array();
$conditions[] = "(quantity > 100)"
if ($var == 0)
$conditions[] = "(id = '$var')";
if (some-other-expression)
$conditions[] = "(myfield = 'foo' OR myfield = 'bar')";
$sql = "
SElECT id, name, quantity
FROM products
WHERE
";
$sql .= $conditions.join(" AND ");
/ Carsten
I would use a ternary for this:
$query = "SELECT id, name, quantity FROM products WHERE quantity > 100" . ( $var != 0 ? " AND id = '$var'" : '' );
I'm an Oracle guy, but could you use the IF() function in your constraints, e.g.:
SELECT id, name, quantity
FROM products
WHERE quantity > 100
AND id = IF('$var'=0,'%','$var');
The '%' is a wildcard that would match anything, effectively ignoring the 2nd expression.
So if you are using the
If or the CASE
Why don't you use them in your sql query.
It would be some like
if ( $var == 0 ) {
$query_without_second_expression
}
else
{
$query_with_second_expression
}
But in you sql query.
DECLARE #var int
if(#var=1)
BEGIN
query 1
END
else
BEGIN
query 2
end
I guess this will solve your problem.
But as a personal advice try to make one query. Even with the variable. We don't believe in changing standard query depending on the conditions.
Still your wish and your desire
Cheers!!
For this particular problem, go with Paper-bat's simple condition append snippet.
However, if you have entirely different where statements, consider appending the appropriate where statement to the query once you know what you want. For instance,
$query = "SELECT id, name, quantity FROM products ";
if ( $var == 0 ) {
$query .= "quantity > 100";
} elseif {
$query .= "quantity > 120 AND id = '$var'";
} elseif {
...
}
...
It all depends on your needs, but neither this or Paper-bat's solutions duplicate query code.

PHP add many rows to database

I have problem with database. I want to add about 10 new rows after pageload. It should check if there is an article with id (that is actually loading). If its not, add 10 of that id with different tag id.
$sprawdz = "SELECT id_artykulow FROM tag_art WHERE id_artykulow='".$_GET['id']."' " ;
$miasto = mysql_query($sprawdz);
$a = mysql_num_rows($miasto);
while ($a<=10){
$zm = "SELECT id FROM tag_content ORDER BY RAND() LIMIT 1";
$sw = mysql_query($zm);
while($row3=mysql_fetch_array($sw)){
$zmienna = "INSERT INTO tag_art(id_artykulow, id_tagow) VALUES ('".$_GET['id']."', '".$row3['id']."' ) ";
$cokolwiek = mysql_query($zmienna);
}
$a++;
}
There is two tables. One tag_content with id (of the tags), and another tag_art with id_artykulow (= id of the article) and id_tagow (id of the tag taken from tag_content)
Don't know why, but it doesn't add 10 rows (it should be for example ten id_artykulow = 10, with different id_tagow). How to fix it?
Thx for help and let me know if u need more informations (like more code etc.)
why do you have 2 while loops?
cant you just replace
while ($a<=10){
$zm = "SELECT id FROM tag_content ORDER BY RAND() LIMIT 1";
with
if ($a<=10){
$zm = "SELECT id FROM tag_content ORDER BY RAND() LIMIT " . (10-$a);
or just let the databse do all the work by:
$query = "INSERT INTO tag_art(id_artykulow, id_tagow) SELECT '".$_GET['id']."', id FROM tag_content ORDER BY RAND() LIMIT " . (10-$a);
(and i would also recomendate protection agains sql-injections, mysql_real_escape_string())
Or let the database do all the work:
// store id as mysql-variable, but let php force it to an integer first
mysql_query("SET #id = " . (int) $_GET['id']);
// calculate how many new art we need, to get 10, using greatest to avoid negative numbers
mysql_query("SELECT #art_needed := GREATEST(0, 10 - COUNT(*)) FROM tag_art WHERE id_artykulow = #id");
// Need to use prepere to be able to have an mysql-variable as limit
mysql_query("PREPARE art_stmt FROM 'INSERT INTO tag_art(id_artykulow, id_tagow) SELECT ?, id FROM tag_content ORDER BY RAND() LIMIT ?'");
// execute the query using the mysql-variables
mysql_query("EXECUTE art_stmt USING #id, #art_needed");
just reread my old answer, and no longer agrees with "or just let the databse do all the work by" for that answer, the database could do alot more.

Categories