$sql = "SELECT users.user_name, users.password, profil.name
FROM users
LEFT JOIN
profil ON users.id = profil.id
WHERE user_name='admin' AND password='123'
ORDER BY profil.name;";
$result = mysqli_query($conn, $sql);
echo $_SESSION['name']
table:user
| user_name| pass | id |
| -------- | -----|-----|
| admin | 123 | 1 |
| admini | 123 | 2 |
table : profil
| id | name |
| ---| --------|
| 1 | ajramos |
| 2 | carlo |
how can i print the name when i log in a account
i wanted to display the name when i log in a acc for example admin 123 ajramos should print
you have to execute the query and save into a session
$sql = "SELECT users.user_name as user_name, users.password, profil.name as name
FROM users
LEFT JOIN
profil ON users.id = profil.id
WHERE user_name='admin' AND password='123'
ORDER BY profil.name LIMIT 1;";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_array($result);
$_SESSION['login_data'] = $data;
echo $_SESSION['login_data']['name'];
Related
Column title has a lot of duplicated values, more than once.
I need to update the column so, for example if 'gold' is duplicated - it becomes 'gold 1', 'gold 2', etc.
Something like this:
$st = $db->query("select id, title from arts order by title asc");
$st->execute();
$x = 0;
while($row = $st->fetch()){
$title = $row['title'];
//if($title.is duplicated){
$x++;
$title .= ' ' . $x;
$stb = $db->query("update arts set title = '" . $title . "' where id = " . $row['id']);
$stb->execute();
}
}
Any help?
It would be more efficient to do this in pure SQL rather than using PHP. Here is an approach that uses window functions, available in MySQL 8.0.
You can use a subquery to count how many title duplicates exists for each record, and assign a rank to each record within groups of records having the same title. Then, you can JOIN the subquery with the table to update. Where more than one record exists, you can append the row number to every record in the group.
Query:
UPDATE arts a
INNER JOIN (
SELECT
id,
title,
COUNT(*) OVER(PARTITION BY title) cnt,
ROW_NUMBER() OVER(PARTITION BY title ORDER BY id) rn
FROM arts
) b ON a.id = b.id
SET a.title = CONCAT(a.title, b.rn)
WHERE cnt > 1;
Demo on DB Fiddle
Sample data:
| id | title |
| --- | ------ |
| 10 | silver |
| 20 | gold |
| 30 | gold |
| 40 | bronze |
| 50 | gold |
| 60 | bronze |
Results after running the update query:
| id | title |
| --- | ------- |
| 10 | silver |
| 20 | gold1 |
| 30 | gold2 |
| 40 | bronze1 |
| 50 | gold3 |
| 60 | bronze2 |
Please see below code that working for me
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// get all row
$sql = "select id, title from arts order by title asc";
$result = $conn->query($sql);
while ($row=$result->fetch_assoc()) {
$title=$row['title'];
// select where title is same
$sql = "select * from arts where title='".$title."'";
$result2 = $conn->query($sql);
// if number of row is greater then one
if ($result2->num_rows > 1){
$x=0;
while ($row2=$result2->fetch_assoc()) {
$id=$row2['id'];
// skip first row
if($x>0){
$newTitle=$title.' '.$x;
$uquery = "update arts set title='".$newTitle."' where title='".$title."' and id=$id";
$update = $conn->query($uquery);
}
$x++;
}
}
}
and after query run
This works in MySql 5.7:
update arts a inner join (
select * from (
select t.id,
(
select count(*) + 1 from arts
where id < t.id and title = t.title
) counter
from arts t
) t
) t on t.id = a.id
set a.title = concat(a.title, ' ', t.counter)
where a.title in (
select h.title from (
select title from arts
group by title
having count(*) > 1
) h
);
See the demo.
For data:
| id | title |
| --- | -------- |
| 1 | silver |
| 2 | gold |
| 3 | diamond |
| 4 | bronze |
| 5 | gold |
| 6 | bronze |
| 7 | gold |
the result is
| id | title |
| --- | -------- |
| 1 | silver |
| 2 | gold 1 |
| 3 | diamond |
| 4 | bronze 1 |
| 5 | gold 2 |
| 6 | bronze 2 |
| 7 | gold 3 |
I think It would be more efficient to do this in SQL too, but you may can do a function to validate the duplicate, something like this:
function isDuplicated( $title, $db ){
$dp = $db->query("SELECT * FROM arts WHERE title = $title");
if ( $dp->num_rows > 1)
return true;
return false;
}
$st = $db->query("select id, title from arts order by title asc");
$st->execute();
$x = 0;
while($row = $st->fetch()){
$title = $row['title'];
if( isDuplicated( $title, $db ) ){
$x++;
$title .= ' ' . $x;
$stb = $db->query("update arts set title = '" . $title . "' where id = " . $row['id']);
$stb->execute();
}
}
I have the following 2 tables.
| ID | Name | Category |
|----|-------------|----------|
| 1 | Foo bar | 3 |
| 2 | Bar foo | 2 |
| 3 | Baz Foo | 3 |
| 4 | Baz Foo2 | 1 |
| 5 | Baz Foo3 | 1 |
| 3 | Baz Foo | 1 |
| ID | Category_name |
|----|---------------|
| 1 | Cat 111 |
| 2 | Cat 222 |
| 3 | Cat 3333 |
I want to display all categories with counter, example:
Cat111 - 3
Cat222 - 2
Cat333 - 2
I tried to do it by the following way, but its not working:
$query = mysqli_query('SELECT * FROM gallery');
while($row = mysqli_fetch_assoc($query)) {
$query_cat = mysqli_query($conn, "SELECT * FROM `pics_cat` WHERE id = '".$row['category']."' GROUP BY category_name");
$rowCat = mysqli_fetch_assoc($query_cat);
echo $rowCat['category_name'];
echo $rowCat['cnt'];
}
You are not sharing the names of the tables, but I assume the first one is Gallery and the second one is pics_cat
If your tables are not going to be very large, I suggest you to solve everything with a single join query, which simplifies the logic of your script.
$query = mysqli_query($conn, 'SELECT p.Category_name,COUNT(g.ID) AS cnt FROM `gallery` AS g LEFT JOIN `pics_cat` AS p ON p.ID = g.Category GROUP BY p.ID');
while($row = mysqli_fetch_assoc($query)) {
echo $rowCat['Category_name'];
echo $rowCat['cnt'];
}
If you prefer to do this with 2 queries in a loop, it's much easier to start from the Category table and then move to the gallery
$query = mysqli_query($conn, 'SELECT * FROM `pics_cat` ORDER BY ID');
while($row = mysqli_fetch_assoc($query)) {
$query_count = mysqli_query('SELECT COUNT(ID) AS cnt FROM `gallery` WHERE Category = '.$row['ID'].'');
$row_count = mysqli_fetch_assoc($query_count);
echo $row['Category_name'];
echo $row_count['cnt'];
}
I have a table named users with a column called user_subs. It looks like this.
In user_subs I have stored the specific users session username. Lets say this specific users name is James.
Now how would I loop through a specific user_subs looking for "James" and remove him from that specific user_subs without removing all the other names.
This is what I have so far and the only problem is, its deleting all the usernames in user_subs instead of just "James".
if(isset($_GET['p_id'])) {
$the_post_id = $_GET['p_id'];
$the_post_author = $_GET['author'];
}
if(isset($_POST['delete_sub'])) {
$username = $_SESSION['username'];
$query = "SELECT user_subs FROM users WHERE username = '{$username}' ";
$select_users_by_id = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($select_users_by_id)) {
$user_subs = explode(',', $row['user_subs']);
foreach($user_subs as $out) {
$query = "UPDATE users SET user_subs = null WHERE username = '{$the_post_author}' ";
$unsubscribe_user = mysqli_query($connection, $query);
echo "Unsubscribed";
}
}
}
THIS IS JUST IN TEST, PREPARED STATEMENTS WILL BE USED BEFORE GOING LIVE
Thank you for your time.
I second the other user's comment about moving this column to a different table. In the meanwhile, if you want to achieve what you are asking for, you can try removing the user name from the column value and update it with the remaining text.
if(isset($_POST['delete_sub'])) {
$username = $_SESSION['username'];
$query = "SELECT user_subs FROM users WHERE username = '{$username}' ";
$select_users_by_id = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($select_users_by_id)) {
$user_subs = str_replace($username . ',', '', $row['user_subs']);
$query = "UPDATE users SET user_subs = '{$user_subs}' WHERE username = '{$the_post_author}' ";
$unsubscribe_user = mysqli_query($connection, $query);
echo "Unsubscribed";
}
}
OPTION-2
$user_subs = explode(',', $row['user_subs']);
$user_subs_new = [];
foreach($user_subs as $out) {
if ($out !== $username) {
$user_subs_new[] = $out;
}
}
$user_subs = implode(',',user_subs_new);
$query = "UPDATE users SET user_subs = '{$user_subs}' WHERE username = '{$username}' ";
$unsubscribe_user = mysqli_query($connection, $query);
echo "Unsubscribed";
}
Let's start over. Let's start here, in fact...
DROP TABLE IF EXISTS users;
CREATE TABLE users
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,email VARCHAR(20) NOT NULL UNIQUE
);
DROP TABLE IF EXISTS user_subs;
CREATE TABLE user_subs
(user_id INT NOT NULL
, sub_id INT NOT NULL
, active TINYINT NOT NULL DEFAULT 1
, PRIMARY KEY(user_id,sub_id)
);
INSERT INTO users VALUES
(1,'b.smyth634#gmail.com'),
(2,'james#gmail.com'),
(3,'f#gmail.com'),
(4,'sally#gmail.com'),
(5,'thomas#gmail.com');
INSERT INTO user_subs (user_id,sub_id) VALUES
(1,5),
(1,2),
(1,1),
(1,4),
(2,1),
(2,2),
(2,4);
SELECT * FROM users;
+---------+----------------------+
| user_id | email |
+---------+----------------------+
| 1 | b.smyth634#gmail.com |
| 2 | james#gmail.com |
| 3 | f#gmail.com |
| 4 | sally#gmail.com |
| 5 | thomas#gmail.com |
+---------+----------------------+
SELECT * FROM user_subs;
+---------+--------+--------+
| user_id | sub_id | active |
+---------+--------+--------+
| 1 | 5 | 1 |
| 1 | 2 | 1 |
| 1 | 1 | 1 |
| 1 | 4 | 1 |
| 2 | 1 | 1 |
| 2 | 2 | 1 |
| 2 | 4 | 1 |
+---------+--------+--------+
SELECT u.*
, GROUP_CONCAT(us.sub_id) subs
FROM users u
JOIN user_subs us
ON us.user_id = u.user_id
GROUP
BY u.user_id;
+---------+----------------------+---------+
| user_id | email | subs |
+---------+----------------------+---------+
| 1 | b.smyth634#gmail.com | 1,2,4,5 |
| 2 | james#gmail.com | 1,2,4 |
+---------+----------------------+---------+
From here we have a choice. We can either DELETE subs we no longer wish to consider, or simply UPDATE them as 'inactive'.
Either way, we just need a DELETE or an UPDATE. So no SELECT needed. In fact a SELECT would, as I mentioned, be counterproductive - because a user may modify the data set in between the execution of the SELECT and the execution of the UPDATE/DELETE. This is known as a 'race condition'.
I am trying to inner join 3 tables that is from OS TICKET Database.
The code I am using is $qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC";
Code is returning:
string(287) "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.patrick.kershner#gmail.com.') ORDER BY qbcd_ticket.ticket_id DESC"
but it is not displaying anything in the while clause:
while ($row = mysqli_fetch_assoc($result)){
echo $row['qbcd_ticket.number]."<br>";}
I am not sure what is going on, or why its not displaying the results.
Can someone check out my code above and verify?
Try to add the number to your selected properties
$qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id, qbcd_ticket.number FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC"
the first table is:
qbcd_ticket:
rows:
ticket_id | number | user_id | user_email_id | status_id | dept_id | and more...
5 | 762086| 2 | 0 | 1| 1 |
the next is qbcd_user_email
rows:
id | user_id | flags | address
2 | 2 | 0 | example#demo.com
the last is: qbcd_user
id | org_id | default_email_id | status | name | created | updated
2 | 0 | 2 | 0 | Patrick Kershner | 2017-03-03 10:44:28 | 2017-03-03 10:44:28
The information that I need to display, is all corresponding Tickets associated with the customer where it = the email address.
the only static variable that will not change is $_SESSION['user_email']; which is logged by logging into the members area.
I have the following database design for a conversation database and its php code to return a conversation list and the last message of
each and every conversation.
users table
user_id | username | password | email
---------------------------------------------------------------------------
1 | Hopewell Mutanda | a3aca2964e72000eea4c5 | hopewe#gmail.com
2 | Hillary Mutanda | 2000eea4c56cb341002a4 | hillary#gmail.com
3 | Fortunate Mutanda | ca2964e72000eea4c56cb | fortu#gmail.com
4 | Nyasha Mupanguri | a3aca2964e72000eea4c5 | n.k.mupanguri#gmail.com
Conversation table
c_id | user_one | user_two | ip | time
----------------------------------------------------------------------------------
1 | 1 | 2 | 3478348924893 | 82738478234
2 | 1 | 3 | 2487348439340 | 74347738439
3 | 1 | 4 | 3473487438494 | 74387438479
4 | 2 | 1 | 3434830430543 | 38483489934
5 | 2 | 3 | 3985398594589 | 43875438758
6 | 3 | 4 | 3878438439954 | 87457485748
conversation_reply table
cr_id | reply | user_id_fk | ip | time | c_id_fk
-----------------------------------------------------------------------------------------------------
1 | Hello how are you | 1 | 274782347843 | 877428742387 | 1
2 | Im fine and you? | 2 | 873784387438 | 287483473847 | 3
3 | How has been your day | 3 | 727728743387 | 342898328938 | 6
********fields with a _fk means they have a foreign key constraint********************
This is my php code to retrieve the conversation list and the last message of each conversation. Unfortunately it is returning
an empty array and i dont know where i am going wrong. The second sql query should be working fine, it is the first one that i am mainly concerned about.
Thats what at least i found out when i tried debugging it
<?php
include 'database.php';
$user_one = "2";
$pdo = Database::connect();
$sql = "SELECT u.user_id,c.c_id,u.username,u.email
FROM conversation c, users u
WHERE CASE
WHEN c.user_one = '$user_one'
THEN c.user_two = u.user_id
WHEN u.user_two = '$user_one'
THEN c.user_one= u.user_id
END
AND (
c.user_one ='$user_one'
OR c.user_two ='$user_one'
)
Order by c.c_id DESC Limit 20";
$q = $pdo->prepare($sql);
$q->execute(array($sql));
$array = array();
$array["details"] = array();
while ($row = $q->fetch(PDO::FETCH_ASSOC)){
$c_id=$row['c_id'];
$user_id=$row['user_id'];
$username=$row['username'];
$email=$row['email'];
$cquery = "SELECT R.cr_id,R.time,R.reply FROM conversation_reply R WHERE R.c_id_fk='$c_id' ORDER BY R.cr_id DESC LIMIT 1";
$result2 = $pdo->prepare($cquery);
$result2->execute(array($cquery));
while ($crow = $result2->fetch(PDO::FETCH_ASSOC)){
$cr_id=$crow['cr_id'];
$reply=$crow['reply'];
$time=$crow['time'];
$details["cr_id"] = $crow["cr_id"];
$details["reply"] = $crow["reply"];
$details["time"] = $crow["time"];
$details["c_id"] = $row["c_id"];
$details["user_id"] = $row["user_id"];
$details["username"] = $row["username"];
$details["email"] = $row["email"];
array_push($array["details"], $details);
}
}
$json = json_encode($array);
echo $json;
Database::disconnect();
?>
Is it possible that what should be there is a Case Expression and not a Case Statement ?
Replace the first where condition, i.e.,the following :
CASE
WHEN c.user_one = '$user_one'
THEN c.user_two = u.user_id
WHEN u.user_two = '$user_one'
THEN c.user_one= u.user_id
END
with this :
(Case When c.user_one = '$user_one' And c.user_one = u.user_id Then 1
Else Case When c.user_two = '$user_one' And c.user_two = u.user_id Then 1
Else 0 End End ) = 1
I don't know what you are trying to achieve, but I think that the case shouldn't be there.
It would be helpful if you explain what is the result you are expecting from the first query.
EDIT
Here is what you can do with your query.
select *
from conversation as c
, (
select cr1.*
from conversation_reply as cr1
, (
select c_id_fk as conversation_id
, max(cr_id) as reply_id
from conversation_reply
group by c_id_fk
) as cr2
where cr1.cr_id = cr2.reply_id
) as cr
where c.c_id = cr.c_id_fk
and (c.user_one = 1 or c.user_two = 1)