Extraction of the data from MySQL using PHP - php

$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?

first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli

$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}

Related

How to check for no result in MySQL query

I want to check for no result in MySQL query and return a no result message to the user. How do I integrate it with my current code?
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
echo '<div class="mydiv">';
if($row['photo']){
echo "<p>" . '<span class="glyphicon" aria-hidden="true"></span>' . '</p>' . '<p>' . "<img src=http://localhost:8888/example/images/" . $row['photo'] . "><hr>";
}else
echo '<p>' . '<span class="glyphicon" aria-hidden="true"></span>' . $row['message'] . '</p>';
echo '<p>' . '~' . $row['username'] . '</p>';
echo '<p>' . $row['datetime'] . ' ' .'(UTC)' . '</p>';
echo '</div>';
}
I want a "no result" message to show up when there are no records found. How do I integrate it in my existing code. Please advise.
if ($row = mysql_num_rows($result) == 0){
echo "<h3>There are no result found.</h3>";
}
if ($result = mysql_query($sql) && mysql_num_rows($result) > 0) {
// there are results in $result
// whatever you want to do with your results
} else {
// no results
echo "<h3>There are no results found.</h3>";
}

Display comments for each post in PHP/mySQL site

I'm trying to display multiple posts, each with its own multiple comments. This, below, works for the first post (or, at least shows 2 of the 3 comments), but not for the rest. I searched but couldn't find (or didn't recognize) an answer to this exact problem on this site, or elsewhere. Thanks.
$result1 = #mysql_query('select * from posts,comments where comment_post_ID = ID and post_status="publish" and comment_approved="1" ORDER BY post_date,category,subcat1,subcat2 ASC');
$result2 = #mysql_query('select * from comments where comment_approved="1" ORDER BY comment_date');
$row = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);
while ($row = mysql_fetch_array($result1)) {
$post = $row['post_content'];
$id = $row['ID'];
$title = $row['post_title'];
$content = "<br /><h3 align=\"left\">" . $title . "</h3>\n";
$content .= "<h4 align=\"left\">by " . $row['post_author'] . " - " . $row['post_date'] . "</h4>\n";
$content .= "<p align=\"left\">" . var_export($post, true) . "</p>\n<p>\n";
echo $content;
while ($row2 = mysql_fetch_array($result2)) {
$comment = "<blockquote>";
$comment .= "<h4 align=\"left\">" . $row2['comment_author'] . " commented on " . $row2['comment_date'] . "</h4>\n";
$comment .= "<p align=\"left\">" . $row2['comment_content'] . "</p>\n<p>\n";
$comment .= "</blockquote>";
if($row2['comment_post_ID'] == $id) {
echo $comment;
}
}
}
This, below, finally worked as hoped:
$postData = mysqli_query($conn,"SELECT * FROM wp_posts ORDER BY post_date ASC) or die(mysqli_error());
$commentData = mysqli_query($conn,"SELECT * FROM wp_comments WHERE comment_approved = '1' ORDER BY comment_date ASC") or die(mysqli_error());
$posts = array();
while($row = mysqli_fetch_assoc($postData)) {
$posts[] = $row;
$commentrow = mysqli_fetch_assoc($commentData);
$comments[] = $commentrow;
echo '<h3>' . $row['post_title'] . '</h3>';
echo '<h5>' . $row['post_author'] . ', ' . $row['post_date'] . '</h5>';
echo '<p>' . $row['post_excerpt'] . '... read more</p>';
if($row['comment_count'] > 0) {
echo '<blockquote>';
echo '<b>Comments</b><br />';
foreach($comments as $comment) {
if($row['ID']==$comment['comment_post_ID']) {
$comment_excerpt = substr($comment['comment_content'],0,100);
echo '<br>' . $comment_excerpt . ' - <b>' . $comment['comment_author'] . '</b>, ' . $comment['comment_date'] . '<br>';
}
}
echo '</blockquote>';
}
}

Displaying the last 3 news messages

I want to display the last 3 news messages onto my PHP page. For that I am using the following code:
function news($number) {
$number = (int)$number
$query = mysql_query("SELECT `id`, `title`, `author`, `message`, `date`
FROM `news`
WHERE `hidden` = 0
ORDER BY `date`
DESC LIMIT $number");
while ($row = mysql_fetch_array($query))
{
return '<p class="p_sub">' . $row['title'] . '~' . $row['author'] .
'</p><p>' . $row['message'] . '</p>';
var_dump($row);
}
}
echo news(3);
However, this only displays one message, not three. Anyone who can figure out why?
Using return will exit your while loop. What you could do is concatenate a string containing your HTML like so
$html = '';
while ($row = mysql_fetch_array($query)) {
$html .= '<p class="p_sub">' . $row['title'] . '~' . $row['author'] . '</p><p>' . $row['message'] . '</p>';
}
return $html;

Sorting nested While() Loops

I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}

MySQL Query with Multiple Select options

I'm a designer and not developer, so I don't even know HOW to ask this question.
I have a select box in my script and it shows only one user type. But I need it to show more user types, for example, user_type 2, but Ive tried adding AND / OR and it doesnt work.
Heres the script code.
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . '
WHERE user_type="3" ORDER BY username ASC', $link) or die(mysql_error());
Heres the full line
<select class=\'widtha\' name=\'contact_user\'>
';
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . ' WHERE user_type IN ("2","3") ORDER BY username ASC', $link) or die(mysql_error());
while ($row = mysql_fetch_array ($result))
{
$row = safe_data ($row, 'display');
echo '<option value=\'' . $row['email'] . '\'';
if ($row[id] == $_POST[contact_user])
{
echo ' selected=\'selected\'';
}
echo '> ' . $row['username'] . '</option>';
}
echo '</select>
Please can someone help me.
Anton
$result = mysql_query ('SELECT id,email,username FROM ' . $dbacct . '
WHERE user_type IN ("1","2","3",....) ORDER BY username ASC', $link) or die(mysql_error());

Categories