the database tables for debat and comments are the same. it seems that $title and $date isnt working. how can i get the information from both, and order by date?
<?php
$showwall = mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') ORDER BY date LIMIT 0, 25");
$results = array();
while($wall = mysql_fetch_array($showwall, MYSQL_ASSOC)){ // Line 21
$results[] = $wall;
<li class="comment">
<div class="comment">
<div class="comment-author vcard user frame">
<?php echo "<img src=images/avatar/".$select['avatar']." class=avatar avatar-70 photo height=70 width=70 />"; ?>
</div>
<div class="message">
<span class="reply-link"><?php echo ""; ?> Edit </span>
<div class="info">
<h2><?php echo $wall['title']; ?></h2>
<span class="meta">
<?php
$date->setTimestamp($wall['date']);
echo "Dato: ";
echo $date->format('d-m-Y') . "\n";
echo "Kl: ";
echo $date->format('H:i:s') . "\n";
?>
</span>
</div>
<div class="comment-body ">
<p><?php echo $wall['comment']; ?></p>
</div>
</div>
</div>
</li>
<?php } ?>
I want to add another database to load from, it seems to leave a blank result.
$showwall = #mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') UNION (SELECT * FROM nyheder WHERE user='$user') ORDER BY date LIMIT 0, 25");
the database contains tables with the same names, but some have more fields than others. So this is isnt the right way to do it?
$showwall = #mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') UNION (SELECT * FROM nyheder WHERE user='$user') ORDER BY date LIMIT 0, 25")
As mentioned in the comments, the # will suppress error messages; those are there for a reason. You're also not checking the result - adding or die (mysql_error()) would give you the error message from the database.
In this case, the issue is because you're using UNIONs. That will only work if all tables have columns in the same order, and I suspect that that isn't the case. I think you just need a simple JOIN:
SELECT * FROM debat d
INNER JOIN comments c ON d.user=c.user
INNER JOIN nyheder n ON d.user=n.user
WHERE d.user='$user'
One final note - you're using mysql_* functions, which are being removed from PHP. You should look at moving to mysqli_* or PDO instead - they both make it easier for you to write safer code.
Related
so I have 3 tables all inner joined, I am trying to put discussions with the discussion topic on my main page, with the users name and the latest discussion message on the main page as well.
THE PROBLEM: The query is returning all messages attached to all the discussions.....I just need the last message associated with the discussion returned.
I have been at this all day and cant figure it out. I have tried group by but that just gave me an error on mysqli_fetch_array
Here is my code
$discussionquery = "SELECT discussion_messages.discussion_id, user_profile.first_name, user_profile.last_name, discussion_messages.message_text, case_discussion.discussion_title
FROM `user_profile`
INNER JOIN discussion_messages
ON (user_profile.user_id = discussion_messages.user_id)
INNER JOIN case_discussion
ON (case_discussion.discussion_id = discussion_messages.discussion_id)
WHERE discussion_messages.case_id = '$thecaseid'
ORDER BY discussion_messages.message_id DESC";
$discussionquerydata = mysqli_query($dbc, $discussionquery);
while ($row2 = mysqli_fetch_array($discussionquerydata)) {
$discussion_id = $row2['discussion_id'];
?>
<!-- Begin Recent Discussions -->
<div class="row">
<a href="discussion.php?newfact=$thecaseid'>">
<div class="col-xs-4 col-md-2">
<img src="img/client4.jpg" class="profile_picture img-rounded">
<?php echo '<h6 class="discussionname">' . $row2['first_name'] . ' ' . $row2['last_name'] . '</h6>';?>
</div>
</a>
<div class="col-xs-8 col-md-10 discussion_title">
<?php echo '<h5> '.$row2['discussion_title'].' -'. $row2['message_text'];?></h5>
<h6 class="pull-right">Dec. 25</h6>
</div>
</div>
<?php
};
You need logic to find the last message for each discussion. There is more than one way to write this condition.
The following does it using a not exists clause. This checks that there are no messages with a larger id for the discussion:
SELECT dm.discussion_id, up.first_name, up.last_name, dm.message_text, cd.discussion_title
FROM `user_profile` up INNER JOIN
discussion_messages dm
ON (user_profile.user_id = dm.user_id) INNER JOIN
case_discussion cd
ON (cd.discussion_id = dm.discussion_id)
WHERE dm.case_id = '$thecaseid' and
not exists (select 1
from discussion_messages dm2
where dm2.discussion_id = dm.discussion_id and
dm2.message_id > dm.message_id
)
ORDER BY dm.message_id DESC;
One suggestion, use table aliases. The amount of total code will be smaller and easier to understand. Use the limit clause to return 1 row.
$discussionquery = "
SELECT
m.discussion_id,
u.first_name,
u.last_name,
d.message_text,
c.discussion_title
FROM `user_profile` as u
INNER JOIN discussion_messages as m
ON (u.user_id = m.user_id)
INNER JOIN case_discussion c
ON (m.discussion_id = c.discussion_id)
WHERE
m.case_id = '$thecaseid'
ORDER BY
m.message_id DESC
LIMIT 1";
Hi everyone i have one question for my dashboard rank panel.
the number of page views I want to sort from smallest to largest.
For example: a total of 100 times a page, but the other page ... 75-74-73-68-45-30 80 times.
I want to get older and smaller than the sequencing of numbers.
My php code is this. post_view is for how many people visit my post .
<?php include("connect.php");
$select_posts = "SELECT * FROM posts LIMIT 0,9";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)){
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_date = date('d-m-y');
$post_author = $row['post_author'];
$post_view = $row['post_view'];
?>
<div class="div_name">
<div class="page-id"><?php echo $post_id; ?></div>
<div class="post_title"><?php echo $post_title; ?></div>
<div class="post-view"><?php echo $post_view; ?> </div>
</div>
<?php } ?>
Use SQL's ORDER BY in the query:
SELECT * FROM posts ORDER BY post_view ASC LIMIT 0,9
If this field is not set to a numerical, you can use the following syntax
SELECT * FROM posts ORDER BY cast(post_view as int) ASC LIMIT 0,9
I'm trying to get my article to fetch how many comments it has, and display the number. Here's the code:
<?php
$amount_get = mysql_query("SELECT * FROM comment WHERE id='" . mysql_real_escape_string($_GET['articleid']) . "'");
$comments = mysql_num_rows($amount_get);
$grab = mysql_query("SELECT id, news_title, news_content, news_date, news_author FROM articles ORDER BY id DESC LIMIT 5");
while($row = mysql_fetch_array($grab)){
?>
<div class="pane">
<li>
<h2><?php echo $row['news_title'] ?></h2>
<div class="meta">
<span class="color"> • </span>
<?php echo $row['news_date'] ?>
<span class="color"> • </span>
</div>
Comments: <?php echo $comments ?>
but for some reason, it stays on "0". This is what it looks like:
and my columns are id, articleid,name,comment,date & ip.
Your where clause is looking for id = articleid.
What you want is where articleid = articleid.
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['articleid']) . "'");
It looks like the SQL is faulty. Check your articleid GET variable. Also should it not be doing a different query and mysql_num_rows for each article's comments as each article likely contains different number of comments.
Try this:
SELECT
articles.id, news_title, news_content, news_date, news_author
(SELECT COUNT(*) FROM comment WHERE comment.id=articles.id) as comments
FROM articles
ORDER BY id DESC LIMIT 5
This will get you the first 5 articles and their related comment counts.
You could just use a single query.
$grab = mysql_query(
"SELECT a.id, a.news_title, a.news_content, a.news_date, a.news_author, count(*) as comment_count
FROM articles a
LEFT JOIN comment c ON c.articleid = a.id
ORDER BY a.id DESC LIMIT 5 GROUP BY a.id");
Then instead of
Comments: <?php echo $comments ?>
Do:
Comments: <?php echo $row['comment_count']; ?>
Let me know if the query is working, I'm not sure if the Group by is correctly placed there.
I'm pulling some data from database, it's a private site so im not too worried about using mysql right now although I do understand I should be using PDO, just haven't made the switch yet:
<table id="table" border="1" bordercolor="#000000" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Exception ID</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Exception</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">First 250 chars of code</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title"># of exceptions</span>
</span>
</th>
<th>
<span class="th">
<span class="arrow"></span>
<span class="icon"></span>
<span class="title">Bug #</span>
</span>
</th>
</tr></thead><tbody>
<?php
//Need to find the total rows in the snippets_link_email_id because well need to show the last x records, so we get total number or rows and then minus the total RECORDS to only select the last x
$total_snippet_check = mysql_query("SELECT COUNT(email_id) as num_rows FROM snippets_link_email_id");
$row = mysql_fetch_object($total_snippet_check);
$total_rows = $row->num_rows;
//finding out how many exceptions there was in the last 24 hours from a table that records total exceptions every hour
$how_many_recent_crashes = mysql_query("SELECT * FROM crash_log_entries ORDER BY crash_id DESC LIMIT 24");
while ($row_recent_crashes = mysql_fetch_array($how_many_recent_crashes))
{
$crash_processed = $row_recent_crashes['crash_processed'];
$crash_processed_total += $crash_processed;
}
$which_records = $total_rows - $crash_processed_total;
//need info on that snippet
$feedback_query_first =
"SELECT *, COUNT(*) AS tot_snippets
FROM snippets
LEFT JOIN snippets_link_email_id
ON (snippets.snippet_id = snippets_link_email_id.snippet_id)
WHERE snippets_link_email_id.email_id > $which_records
GROUP BY snippets.snippet_id
ORDER BY tot_snippets asc";
$result1 = mysql_query($feedback_query_first);
while ($row1 = mysql_fetch_array($result1))
{
$i = $row1['snippet_id'];
//Need to find the total snippets for the current snippet
$feedback_query = mysql_query(
"SELECT * FROM snippets
LEFT JOIN snippets_link_email_id
ON snippets.snippet_id = snippets_link_email_id.snippet_id
WHERE snippets_link_email_id.snippet_id = $i
AND snippets_link_email_id.email_id > $which_records");
$tot_snippets = mysql_num_rows($feedback_query);
$snippet_text_pre = $row1['snippet_text'];
$snippet_text_pre1 = htmlspecialchars($snippet_text_pre);
$snippet_text = str_replace("<br />", "<br />",$snippet_text_pre1);
$snippet_text = substr($snippet_text,0,250);
$comment = $row1['comment'];
$comment_short = substr($comment, 0, 35);
$note_length = strlen($comment);
$snippet_id = $row1['snippet_id'];
$email_id = $row1['email_id'];
$query_exceptions = "SELECT * FROM emails WHERE email_id = $email_id ORDER BY email_id DESC";
$result2 = mysql_query($query_exceptions);
while ($row2 = mysql_fetch_array($result2))
{
$actual_exception = $row2['actual_exception'];
}
echo '<tr><td>'.$snippet_id.'</td>';
echo '<td>'. $actual_exception.'</td>';
echo '<td>'. $snippet_text.'....</td>';
echo '<td>'. $tot_snippets.'</td>';
$tot_tot += $tot_snippets;
echo '<td>'. $comment . '</td></tr>';
}
echo "</tbody></table>";
echo "the total exceptions for this time period is: " . $tot_tot . "<br />";
?>
Ok hopefully that is ok, I filtered out everything irrelevant so hopefully that code makes some sense. Now is there a way to sort by $tot_snippets by default when the page loads without using a full jquery sorting solution as I only want to sort this by this column always as I don't need to update it? I don't think I can sort this using orderby since the value of tot_snippets is not a column value but all the solutions I seem to find have a full blown sorting solution and most involve Jquery, if Jquery is the best option so be it but I thought there might be an easier way?
//Edit
I updated the code and added basically the whole code, I realize my code is messy, I'm self taught and just learned enough to do what I need to do. I'm sure it could be altered to be a lot more compact but my main issue is even with one of the previous solutions it still doesn't sort by the # of exceptions column, I'm guessing the solutions giving were good by my leaving out some of the other code meant it didn't work so I decided to include the whole code this time. I originally left it out to make it less complicated but I now realize that probably was counter productive.
You can actually combine these two queries:
SELECT snippets.snippet_id,COUNT(snippets_link_email_id.id) FROM snippets
LEFT JOIN snippets_link_email_id ON snippets.snippet_id = snippets_link_email_id.snippet_id
WHERE snippets_link_email_id.snippet_id = $i
AND snippets_link_email_id.email_id > $which_records
GROUP BY snippets.snippet_id
Which means you only have to do one query rather than (numsnippets + 1) queries. This also allows ordering by the count by adding:
ORDER BY COUNT(snippets_link_email_id.id)
To the end of the query.
In my opinion you can skip the first query. and get the distinct id's in the second query
using a group by snippet_id.
Then you can add a count to the query (i used the alias tot_snippets)
and order by the alias tot_snippets.
Then you get a query like this
select
*, count(*) as tot_snippets
from snippets
left join snippets_link_email_id on(snippets.snippet_id = snippets_link_email_id.snippet_id)
where snippets_link_email_id.email_id > $which_records
group by snippets.snippet_id
order by tot_snippets asc
To make your query more readable use aliases on the tables and name the column snippet_id in table snippets just id this makes it easier to understand your query
I am trying to combine 2 tables from my database:
files table:
id
file_name
file_description
file_url
access_files table:
id
student_id
file_id
Here is my sql code, currently getting all files from the files table, it doesn`t show the selected files for the user.
<?php
$SQL = "SELECT * FROM files, access_files WHERE student_id ='$studentid'";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
?>
<div class="accordion-group">
<div class="accordion-heading">
<a href="#<?php print $db_field['file_id']; ?>" data-parent="#accordion" data-toggle="collapse" class="accordion-toggle collapsed">
<?php print $db_field['file_name']; ?>
</a>
</div>
<div class="accordion-body collapse in" id="<?php print $db_field['file_id']; ?>">
<div class="accordion-inner">
<?php print $db_field['file_description']; ?><br/><br/>
Download File Now!
<br/><br/>
</div>
</div>
</div>
<?php } ?>
The code is suppose to show only the files associated to the user.
What you need to do is JOIN the tables.
Most common types of JOINs:
INNER JOIN - Used to match data where it can match the ON () statement. If the ON() doesn't match, the result will be excluded.
LEFT JOIN - Used if the data you match in the ON() doesn't have to be there. It just appends the data to the original FROM, and fills columns with NULL if no data is matched.
Example
SELECT
ft.id,
ft.file_name,
ft.file_description,
ft.file_url,
af.id as access_id,
af.student_id,
af.file_id
FROM
files ft
INNER JOIN access_files af ON ( ft.id = af.file_id )
WHERE
fa.student_id = '$studentid'
You are making a classic cartesian join with your query:
SELECT * FROM files, access_files WHERE student_id ='$studentid'
You need to specify how the two tables are connected:
SELECT * FROM files a, access_files b WHERE a.student_id ='$studentid' and b.studentID=a.student_id
If you don't specify the link - or don't have one, the database will try to link every single row in the first table with every single row in the second.
Join your tables.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table1.pk = table2.fk
WHERE table1.pk = 1;