I have two table called t_user and t_chat. I am trying to display message from t_chat in PHP table like below.
$quotes_qry="SELECT * FROM t_chat ORDER BY id DESC LIMIT $start, $limit";
$result=mysqli_query($mysqli,$quotes_qry);
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo ++$sr+$start;?></td>
<td><?php echo $row['sender'];?></td>
<td><?php echo nl2br($row['receiver']);?></td>
<td><?php echo nl2br($row['message']);?></td>
<td><?php echo time_elapsed_string($row['time']);?></td>
<td><img src="images/delete-icon.png"></td>
</tr>
<?php
$i++;
}
?>
I want display sender and receiver name which is located in table called t_user with username column. I am new in PHP and confused how can I achieve it. Let me know if someone can help me for achieve my task. Thanks a lot!
Note : t_chat table have userid witch column name called sender and receiver, currently I am displaying that userid in above table, instead I want display username.
Thanks
The joins might look something like this:
SELECT t_chat.sender AS 'sender', t_chat.receiver AS 'receiver', t_chat.message AS 'message', t_chat.time AS 'time', t_chat.id AS 'id', user1.username AS 'senderusername', user2.username AS 'receiverusername'
FROM t_chat
LEFT JOIN t_user AS user1 ON t_chat.sender = user1.id
LEFT JOIN t_user AS user2 ON t_chat.receiver = user2.id
ORDER BY id DESC
In this example I am joining the tables twice (as user1 and user2) so that the t_user table gets referenced independently for each lookup.
I also gave each column a name using AS to make them easier to reference later in your code.
Try this sql (The t_user table must have an id column that matches the userid from t_chat):
$quotes_qry="SELECT t1.sender, t1.receiver, t1.message, t1.time, t2.username FROM t_chat AS t1 INNER JOIN t_user AS t2 ON t1.userid=t2.id ORDER BY t1.id DESC LIMIT $start, $limit";
More details and examples about MySQL JOIN you can find in the tutorial from:
https://coursesweb.net/php-mysql/mysql-inner-left-join-right-join_t
Related
Right, hello.
I've been working on a website template and I've coded a forum-like section, but simply discussion threads, and now, I'm having struggles with a specific query. This.
SELECT * FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10
I thought it would be easy enough to see how others have their question answered on how to order one table by another table's row. So I tried their method, but now it's mixing it up with each other. This is how it used to look like when I was sorting the threads by their IDs
SELECT * FROM cms_discussions ORDER BY id DESC LIMIT 10
And this is how it looked.
While using the SQL query I first provided, the outcome came out kinda wrong. The author of the thread was changed to 'server' (which was the author field from cms_discussions_comments), so basically, the 'author' field from cms_discussion gets replaced with the 'author' field from cms_discussions_comments. How I display the threads are quite simply.
<?php
$response = $databaseHandle->query("SELECT * FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10");
while($row = $response->fetch_assoc()) {
stripslashes_array($row);
?>
<table class="row" onclick="window.location.href = '/discussion/<?php echo $row["id"]; ?>/';">
<tr>
<td style="width: 50px"><img src="/styles/<?php echo $styleConfig["theme"]; ?>/album/discussions/<?php echo ($row["active"] == 1)?("status_info"):("status_locked"); ?>.png"></td>
<td><p id="title"><?php echo $row["title"]; ?></p><br><p id="description">Posted <?php echo date("F j, Y, g:i A", $row["posted"]); ?> by <i><?php echo $row["author"]; ?></i>.</p></td>
</tr>
</table>
<?php } ?>
<?php } ?>
I'm really not sure what the issue is here since I usually just do basic queries. Thanks in advance for any assistance.
Edit: Forgot to provide the second screenshot:
Since your two tables cms_discussions and cms_discussions_comments both contain an author field, one of the twos is not going to be accessible in your result array if you just do SELECT *.
What you need to do is to explicitly select fields and assign aliases so that there is no overlaps.
You could do something like:
SELECT d.*, c.author AS comment_author FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10
With that query, your $row['author'] would refer to the field of cms_discussions while $row['comment_author'] would refer to the field of cms_comments. That being said, you might want to do the same for other fields like id.
If you simply do not want to select columns from cms_discussions_comments then this will do the trick:
SELECT d.* FROM cms_discussions d JOIN cms_discussions_comments c ON d.id = c.discussionid ORDER BY c.time DESC LIMIT 10
I am displaying all data from a table in a view for the administrator in a web app.
The SQL looks something like this:
$organizations = $db->query("
SELECT id, organization_name, owner_id
FROM organizations
ORDER BY created_on DESC
")->fetchALL(PDO::FETCH_ASSOC);
The part of the view I am working with is as follows:
<?php foreach($organizations as $organization): ?>
<tr>
<td><?php echo e($organization['organization_name']); ?></td>
<td><?php echo e($organization['owner_id']); ?></td>
</tr>
<?php endforeach; ?>
This works exactly as expected but is not actually what I want to display as far as the owner_id (an int and the primary key of the users table)
This will generate a table with all the values as in the SQL statement and in particular it will render the owner_id to the view which is a foreign key related to my users table.
What I want to do is actually display the name of the owner that belongs to the owner_id instead of just showing the id (i.e... 32). How can I display the associated name of the user from the users table based on the foreign key user_id that is referenced?
You need to use a JOIN to link the two tables. The example below links the two tables on owner_id and includes the user_name in the result. You will need to use an alias in the SELECT in the case that any of the column names exist in both tables.
-- use alias and include user_name
SELECT o.id, o.organization_name, u.user_id, u.user_name
-- alias the table as "o"
FROM organizations o
-- alias the table as "u"
JOIN users u
-- link the tables here on owner_id
ON o.owner_id = u.user_id
ORDER BY o.created_on DESC
You can then output the value of the user_name column in your PHP like so:
<td><?php echo e($organization['user_name']); ?></td>
You can use JOIN.
$organizations = $db->query("
SELECT organizations.id, organizations.organization_name,
users.user_name
FROM organizations
JOIN users ON organizations.owner_id = users.user_id
ORDER BY organizations.created_on DESC
")->fetchALL(PDO::FETCH_ASSOC);
Then in view, it can be used as
<?php foreach($organizations as $organization): ?>
<tr>
<td><?php echo e($organization['organization_name']); ?></td>
<td><?php echo e($organization['user_name']); ?></td>
</tr>
<?php endforeach; ?>
I have two cycles in PHP, that I needed converting to smarty structure. Down includes PHP code.
Code:
<pre>
$query = mysqli_query($cnn, "SELECT *, COUNT(*) AS ph FROM course INNER JOIN completed_course ON course.id = completed_course.id_course GROUP BY course.id");
while ($row = mysqli_fetch_array($query)){
<tr> <td> <?php echo $row['id']; ?></td><td> <?php echo $row['nazev']; ? </td><td>
?php echo $row['ph']; ? </td> <td>
?php
$Number_of_graduates = mysqli_query($cnn, "SELECT COUNT(*) AS abs FROM participant where id_completed_course = $row[id]");
while ($rAbs = mysqli_fetch_array($Number_of_graduates)){
echo $rAbs['abs'];
} ?
</td>
</pre>
The question is. How to convert the second loop where first id from SQL?
Ok, so your question is really about SQL. Let's look at your queries. The first one looks like this:
SELECT *, COUNT(*) AS ph
FROM course
INNER JOIN completed_course ON course.id = completed_course.id_kurz
GROUP BY course.id
I'm assuming (because I don't know anything about your database scheme) that this is going to give a list of courses and the number of students (maybe - I don't know what's in the completed_course table) who completed them. As written, this query is going to also give you some data from the completed_course table, but it's likely to be meaningless since you're grouping only on course.id.
The second query is:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course = {DATA FROM FIRST QUERY}
Presumably, this query is intended to give you the total number of participants of completed courses. To make that work, the WHERE clause could look like this:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course IN (
SELECT course.id FROM
FROM course
INNER JOIN completed_course ON course.id = completed_course.id_kurz
)
Notice that I'm taking the values selected in the first query and making them part of an IN clause. This query can be simplified significantly - for instance, the JOIN in the subquery is really only selected ID values from the completed_course table:
SELECT COUNT(*) AS abs
FROM participant
WHERE id_completed_course IN (
SELECT id_kurz
FROM completed_course
)
And the query will actually be more efficient if you get rid of the subquery altogether and just join the participants to the completed_course table.
SELECT COUNT(*) AS abs
FROM participant
INNER JOIN completed_course ON participant.id_completed_course=completed_course.id_kurz
This last query is going to give you one value: the number of participants whose id_completed_course value corresponds to an item in the completed_course table. You can use the mysqli methods to retrieve this data and pass it to your Smarty template.
Here i have implemented two tables (pages, pagestatistics).I need result for last five records using join from the those tables and also i need a count of cID field from second table (pagestatistics), i need that count result will be display in descending.
NOTE : Primary id is cID.
![pagestatistics][1]![pages][2]
<?php
$recentpageviews =mysql_query("SELECT Distinct(cID) FROM `pagestatistics` order by `pstID` desc limit 0,5");
$downloads=mysql_num_rows($recentpageviews);
$k=1;
$cid="";
while($views_values=mysql_fetch_array($recentpageviews))
{
$cid.=",".$views_values['cID'];
$k++;}
}
$explode =explode(",",$cid);
for($i=1;$i<count($explode);$i++)
{
$sql=mysql_query("select Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date from pagestatistics as a left join pages as b on a.cID=b.cID where a.cID='".$explode[$i]."' order by a.cID desc");
$res=mysql_fetch_array($sql);
?>
<tr>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['cFilename'];?></td>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['clicks'];?></td>
<td class='ccm-site-statistics-downloads-title'><?php echo $res['date'];?></td>
</tr>
<?php }?>
How to display all records in descending order.
Thank in advance.
You can do it in this way just order by with your count separated with , in ORDER BY clause
$sql=mysql_query("select Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date
from pagestatistics as a left join pages as b on a.cID=b.cID
where a.cID='".$explode[$i]."' order by a.cID,clicks desc");
I have two tables, classified and fordon.
classified table:
classified_id (PK)
etc...
fordon table:
id (PK)
classified_id (FK)
I try to use this code:
SELECT * FROM classified, fordon WHERE classified.ad_id IN ('$solr_id_arr_imploded') AND classified.classified_id=fordon.classified_id
BTW, the array is a set of ad_id:s returned from solr, never mind that, that is not the problem here...
Then I use mysql_fetch_array in a while-loop to display all the results:
while($row = mysql_fetch_array($qry_result)){
but when I try to echo something which is inside the table fordon, then the index can't be found error appears. But whatever is inside the table classified works to echo!
Any ideas?
Thanks
UPDATE
while($row = mysql_fetch_array($qry_result)){
echo $row['type']; // This doesn't work, because the 'type' column is inside the 'fordon' table
echo $row['headline']; // This does work because it's inside 'classified' table.
Does this help?
SELECT *
FROM classified c
INNER JOIN fordon f ON c.classified_id=f.classified_id
WHERE classified.ad_id IN ('$solr_id_arr_imploded');
Also, its generally not a good idea to use: SELECT *. Its better to either select only the elements you want or use the * in context of the table you are getting all from, e.g.
SELECT classified.*
FROM classified c
INNER JOIN fordon f ON c.classified_id=f..classified_id
WHERE classified.ad_id IN ('$solr_id_arr_imploded');
When you do joins with a blanket * you get every field in all tables.