check if exist inside two column - php

need a help with a PHP/MySQL issue. I have a table named bids and two column named buyer and tagged both using int.
buyer
--------------
8
5
2
tagged
--------------
5
4
1
I'm trying to detect multiple same entry number. I want if a same number appears on both of the column it shouldnt display on the menu list anymore, hope yo understand.
Any tip?
code below
$query = "SELECT b.auction, b.buyer, b.bid, b.bidwhen, b.quantity, b.willwin, b.tagged, b.balance, u.nick, u.rate_sum FROM " . $DBPrefix . "bids b
LEFT JOIN " . $DBPrefix . "users u ON (u.id = b.bidder) WHERE b.auction = :auc_id
ORDER BY b.bid asc, b.quantity DESC, b.willwin asc"; $params = array(); $params[] = array(':auc_id', $id, 'int');

Related

Trying to count how many certain types of award a dog has in results table

I have a dog show results website. I am trying to count and show how many certain types of award a dog has.
There are two tables.
Table 1
Result table - with ResultID, dog_id and Award (for example I am trying to find out how many strings that match 'DCC' in award column).
Table 2
Dog table - with dog_id.
I have tried an inner join but it counts is not working correctly. For example, the count should show 1 as I know a certain dog has only 1 'DCC' but it shows 2505 not sure where it's getting this figure from?
<?php
$query = "SELECT result.resultID, result.dog_id, result.award, COUNT(*) AS dcc
FROM result INNER JOIN dogs
ON result.dog_id = dogs.dog_id
GROUP BY result.dog_id, result.award
ORDER BY dcc";
$select_all_dcc = mysqli_query($connection, $query);
$dcc_counts = mysqli_num_rows ($select_all_dcc);
echo "<strong class='amount'>{$dcc_counts}</strong>"
I managed to do it with this code
if (isset($_GET['dog_id'])) {
$query = "SELECT result.* , dogs.dog_name, shows.show_title, shows.show_id
FROM `result`
INNER JOIN `dogs` ON result.dog_id = dogs.dog_id
INNER JOIN `shows` ON result.show_id = shows.show_id
AND result.dog_id = " . $_GET['dog_id'] . "
WHERE award = 'DCC' OR award = 'BCC'";
$select_all_dcc = mysqli_query($connection, $query);
$dcc_counts = mysqli_num_rows ($select_all_dcc);
echo "<strong class='amount'>{$dcc_counts}</strong>";
}

delete item the order changes php

I have a page that has a column "order". When I add a new item the order should be 1 + max of the item. But if I delete an item between these values like i have 8 9 10 , if I delete item with 9 the new item will have 11 value and I will have 8 10 11.But I want 8 9 10. How can I do this in php? should I create a function? It works to add a new item with the max+1, but if I delete is not good.
My code:
$queryMaxOrd = mysql_query("select max(ord) from article where user_id=" .$_SESSION['userId']);
$row = mysql_fetch_row($queryMaxOrd);
$valueMaxOrd = $row[0] + 1;
$_SESSION['a'] = $valueMaxOrd;
and the insert query....
Any ideas?
First thing to consider is removing mysql_* and start using mysqli_* or pdo. Your code is vulnerable to sql injections.
With your code structure you have you adding each order as a new record to your table, so when you have 5 orders with one users your table will look like this.
user: Adiiia | order: 5
user: Adiiia | order: 3
user: Adiiia | order: 7
user: Adiiia | order: 2
user: Adiiia | order: 9
when you query the database you are saying to mysql: get me the maximum number of the record i have with user: Adiiia. The result should be 9
If you want to count the sum of the records you should use sum
select sum(ord) from article where user_id='".$_SESSION['userId']."'
The result should be 26
The best thing you can do is by creating a new table orders_sum
The structure should like like this.
Total_orders | User
when a user have a new order you can update the table by adding one or else if have a order removed then removing minus one order.
Something like this.
Total_orders | User
5 | Adiiia
When you want to update:
select Total_orders from orders_sum where User='Adiiia'
Find how many orders the user have by fetching the table.
$orders = $row['Total_orders'];
$plus_orders = $row['Total_orders'] + 1;
update orders_sum set Total_orders='".$plus_orders."' where user='Adiiia'
When you want to delete:
select Total_orders from orders_sum where User='Adiiia'
Find how many orders the user have by fetching the table.
$orders = $row['Total_orders'];
$minus_orders = $row['Total_orders'] - 1;
update orders_sum set Total_orders='".$minus_orders."' where user='Adiiia'
Lets say article table having primary key article_id then after delete any article for user_id. Fetch all the article_id for the user_id. Then update the order for all the article_id for that user.
$queryArticle = mysql_query("select article_id from article where user_id=" .$_SESSION['userId']." order by article_id asc");
$allArticle = [];
while($rowArticle = mysql_fetch_assoc($queryArticle)){
$allArticle[] = $rowArticle['article_id'];
}
$query = "UPDATE article SET ord = (CASE order ";
foreach($allArticle as $order => $article_id) {
$query .= " WHEN {$article_id} THEN {$order}";
}
$query .= " END CASE) WHERE article_id IN (" . implode(",", $allArticle) . ")";

Working with 2 tables PHP & MySQL

I am working on a webpage that displays list of shops. I have 2 tables, shops and shops_sched.
+-shops-+
| id | title |
+-------------shops_sched-------------+
| id | shops_id | start_date | end_date |
Basically, the program displays the list of shops from the shops table, but if a value from shops.id is found # shops_sched.shops_id the page must output shops.title + 'coming soon'.
I understand this will be easy if I just place the date fields inside the table shops but due to programming restrictions I can't. I'm working on an existing project and I'm trying to minimize changes to existing functions. I can create new PHP functions if necessary though.
In addition, I need to get all the entries from the shops table. The Program needs to return all shops.title but for those shops whose id is found # shops_sched.shops_id, the program will have to return shops.title + "Coming Soon".
must output shops.title + 'coming soon'.
So do it like this:
$shops.title = "Donut-John";
echo $shops.title." coming soon";
To join the shops and shops_sched table
$query = SELECT `title` FROM `shops` JOIN `shops_sched` ON `shops`.`id` = `shops_sched`.`shops_id` WHERE `shops_sched`.`shop_id` = 5;
$result = mysql_query($query);
while($row = mysql_fetch_array($result) {
echo $row['title'] . 'coming soon';
}
For more about join you also can refer the following link
https://dev.mysql.com/doc/refman/5.0/en/join.html
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm
Join the two tables :
SELECT shops.title
FROM shops INNER JOIN shops_sched ON shops.id = shops_sched.shops_id
The query should return only the the shops inside shops_sched
EDIT :
If I understood your question, try this :
SELECT shops.title, shops_sched.id
FROM shops LEFT JOIN shops_sched ON shops.id = shops_sched.shops_id
This will return all the titles, and the shops_sched.shops_id if shops.id = shops_sched.shops_id. In the other case, the hops_sched.shops_id will be null
Then you fetch the rows and if the second row is not null, print title + coming soon
Sample code : (Something like this)
$query = "SELECT `title`, 'shops_id' FROM `shops` LEFT JOIN `shops_sched` ON `shops`.`id` = `shops_sched`.`shops_id` WHERE `shops_sched`.`shop_id`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result) {
if($row['shops_id'] != "")
{
echo $row['title'] . ' coming soon';
}
else
{
echo $row['title'];
}
}

Mysql array and data

Mysql query and PHP code that I'm using to get users from the database that meet certain criteria is:
$sql = mysql_query("SELECT a2.id, a2.name FROM members a2 JOIN room f ON f.myid = a2.id
WHERE f.user = 1 AND a2.status ='7' UNION SELECT a2.id, a2.name FROM members a2
JOIN room f ON f.user = a2.id WHERE f.myid = 1 AND a2.status ='7' GROUP BY id")
or die(mysql_error());
while ($r = mysql_fetch_array($sql))
{
$temp[] = '"'.$r[0].'"';
}
$thelist = implode(",",$temp);
The query that follows get the list of members with new galleries by using array from the previous query.
$ft = mysql_query("SELECT id, pic1 FROM foto WHERE id IN ($thelist) AND
pic1!='' ORDER BY date DESC LIMIT 10");
while ($f = mysql_fetch_array($ft))
{
echo $f['id']." - ".$f['pic1']."<br/>";
}
These queries working fine but I need to get the name for every user listed in second query. This data is in the first query in the column name. How can I get it listed beside '$f['id']." - ".$f['pic1']'?
While I might just alter the first query to pull the galleries at the same time, or change the second query to join and get the name, you could keep the same structure and change a few things:
In the loop after the first query when building $temp[], also build a lookup table of user id to user name:
$usernames[$r[0]] = $r[1];
Then in your output loop, use the id (assuming they are the same!) from the second query to call up the user name value you stored:
echo $f['id'] . " - " . $f['pic1'] . " - " . $usernames[$f['id']] . "<br/>";

mysql select and where over several tables (very tricky)

I have 4 Tables (listed bellow) and need:
get last 10 Chats from Room 3 without banned users
show nickname for fromuserid
HIDE Users $userid dont like to see table "HIDE"
Table 1 "chats"
ID(autoinc) fromuserid roomid text
1 23 3 bla
2 14 1 bla
3 11 3 bal
Table 2 "user" /shorted/
ID(autoinc) nickname banned
1 chris 0
2 paul 1 // 1 = banned
Table 3 "hide"
ID(autoinc) orguser hideuser
1 12 3
2 33 12
Right now i solved it with PHP Routine, but I have to go through EACH result and make always a new query, that needs too long;
$userid = 1; // actual user
// List all chats and show userid as nickname
$sql_com = "SELECT user.id, user.nickname, chats.text, chats.id ".
" FROM chats, user".
" WHERE ".
" chats.fromuserid = user.id ".
" AND chats.roomid = 3 ".
" AND user.banned != 1 ".
" ORDER BY chats.id DESC";
$result = mysql_query ($sql_com);
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$dontshow = false;
// Filter : dont show users $userid dont like to see (table "hide")
$sql_com2 = "SELECT id from hide WHERE ( (orguser = ".$userid.") AND (hideuser = ".$row[0].") ) ";
if ($result2 = mysql_query ($sql_com2))
{
if (mysql_num_rows($result2) > 0) $dontshow = true;
}
// Output
if ($dontshow == false)
{
$count++;
echo "Nickname: ".$row[1]." Text: ".$row[2];
}
if ($count > 10) break;
}
Btw. I made already some improvments, so the actual question may not fit with all answers (thanks for your help till now)
Finaly its now just about to integrate the filter "dont show people listed in table "hide" for my actual user".
I think you need something along these general lines. I've done it slightly different from your question. Instead of getting the top 10 then removing records. It gets the top 10 records which would not be hidden.
SELECT c.ID, c.fromuserid, c.roomid, c.text, u.nickname
FROM chats c
JOIN user u ON c.fromuserid = u.id
where c.roomid = 3 AND user.banned = 0
AND NOT EXISTS(
SELECT * FROM hide h
WHERE h.hideuser = c.fromuserid
AND orguser = $userid)
ORDER BY c.ID DESC
LIMIT 0,10
Not tested but it would be something like:
$sql_com = "SELECT us.id, us.nickname, ch.text, ch.id ".
" FROM chats ch, ".
" user us, ".
" hide hi, ".
" banned ba, ".
" WHERE ".
" us.id != hi.hideuser ".
" us.id != ba.user ".
" us.id = ch.fromuserid ".
" AND ch.roomid = 3 ".
" ORDER BY ch.id DESC LIMIT 0,10";
Although I can't immediately find a simple way to answer your question as-is, I can point you in the right direction:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Using subqueries should enable you to go and select from both blocked and hidden tables, and using those in your original query.

Categories