Get & diplay from database error - php

I am trying to write a simple stream, streaming everything from a mysql field. But my current script shows absolutely nothing.. no errors, nothing. Here it is:
include("user_sytem_scripts/connect.php");
$sql_updates = mysql_query("SELECT item_id, username, item_content, update_time FROM updates ORDER BY update_time DESC LIMIT 30") or die("Query failed with error: ".mysql_error());
while($row = mysql_fetch_array($sql_updates)){
$update_id = $row["item_id"];
$update_username = $row["username"];
$item_content = $row["item_content"];
$update_time = $row["update_time"];
$updatestream = '
<table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%">
<tr>
<td width="90%" valign="top" style="line-height:1.5em;">
<span class="liteGreyColor textsize9">' . $update_time . ' <strong>' . $username . '</strong> via <em></em></span><br />
' . $item_content . '
</td>
</tr></table>'; }
Then down in the HTML I use: <?php echo $updatestream ?>
But as i said i get absolutly nothing.. Can anyone spot any errors or general mistakes that would cause this? Thanks :D

I would check the following:
Does the query run when you run it manually (in phpMyAdmin for example)?
Do you get any output at all? If not, it seems that your while look is getting 0 results from the DB.
You should also change: $updatestream = ' to $updatestream .= so that you append text, in the current while loop you'll overwrite the last $updatestream value.

First you need to initialize $updatestream,($updatestream = ""), before the while loop, then inside the loop you change it to $updatestream .= "the string". You might want to echo your result at the end, sometimes we forget the little stuff.
echo $updatestream;

Related

php while loop echoing element outside of looped content

The forum pages on my website use PHP to create a table and then use a while loop to populate it from the database. This works fine and always has but I have tried to move the anchor, 'link', tag from around the post's title to the entire first section of the post within the table. To do this it goes through the following steps:
Open the table tag [OUTSIDE OF LOOP]
Echo headers [OUTSIDE OF LOOP]
Start WHILE loop that makes another post section for every post found.
Create table row
Create table data
Echo content
Close table data
REPEAT STEPS 5-7 ONCE MORE for post date section
Close table row
close table [OUSTIDE OF LOOP]
It should make the links clickable on all of the first section and they should be within the table like this:
<table> <--- *THIS IS BEFORE THE LOOP, IT GETS RUN ONCE ONLY* -->
<WHILE *do this like 5 times or something*>
<tr>
<a *category link*>
<td>
*content for the 'td' which is taken from the DB*
</td>
<td>
*content for the 'td' which is taken from the DB*
</td>
</a>
</tr>
<ENDWHILE>
</table>
However, in practice they end up outside of the table as can be seen in this screenshot:
Could anyone please explain this and how to fix it?
echo '<table class="forumTable">
<tr>
<th>Category</th>
<th>Last topic</th>
</tr>';
while($row = mysqli_fetch_assoc($catResult)){
echo '<tr>';
echo '<a href="category.php?id=' . htmlspecialchars($row['catID']) . '"><td class="catDesc">';
echo '<h3>' . $row['catName'] . '</h3>' . $row['catDesc'];
echo '</td>';
echo '<td class="catTime">';
$getTops = "SELECT topicID, topicSubject, topicDate, topicCat FROM topics WHERE topicCat = " . $row['catID'] . " ORDER BY topicDate DESC LIMIT 1";
$topResult = mysqli_query($connect, $getTops);
if(!$topResult){
echo '<p style="margin-top: 75px;">The last topic could not be displayed, please try again later.</p>';
}
else{
if(mysqli_num_rows($topResult) == 0){
echo '<p>No topics</p>';
}
else{
while($topRow = mysqli_fetch_assoc($topResult)){
echo '' . $topRow['topicSubject'] . ' at ' . $topRow['topicDate'];
}
}
}
echo '</td></a>';
echo '</tr>';
}
echo '</table>';
Since the source page confirms that the anchors are where you placed them, but the browser moves them around, you can either :
- contain your links inside the td table cell
- use an alternative approach to add the link where you want it html - table row like a link
Did you try to get page not from browser? How it looks?
I think browser does not allow you to put <a> into <table> directly without <tr><td> </td></tr>

PHP MySQL Database multi filtration

So I have a table with a whole load of properties and have displayed them on a webpage. As we have a wide range I developed a filtration system. However when used you can only select one tag, but we want to devise it in a way so that you can select one tag and then another. My current code is as follows.
<h3>Advanced Search:</h3>
<table>
<tr>
<td colspan="2" style="vertical-align: top; width:50%; !important;">
<h3>Location</h3>
<div>
Alsancak
Karsiyaka
Lapta
Kayalar
Sadrazamkoy
Cambeli
Baspinar
<!--
-->
</div>
</td>
<td style="vertical-align: top; width:25%;">
<h3>Number of Bedrooms</h3>
<div>
1
2
3
4
</div>
</td>
And it goes on with another few options the code then on the page is as follows:
<h1>Properties</h1>
<?php
$loc = $_GET["loc"];
$con = mysql_connect("localhost","Username","Password");
mysql_selectdb("db",$con);
$sql = "SELECT * FROM Properties WHERE Location='$loc' ORDER BY `Properties`.`Price` ASC";
$mydata = mysql_query($sql,$con);
echo "<h4>Location:" . $loc . "</h4><br>";
while($record = mysql_fetch_array($mydata)){
echo "<div id=\"property\">" . "<img src=\"images/" . $record['MainPic'] . "\" id=\"propimg\" align=\"left\">" . "<div id=\"text\" style=\"text-align: center; margin-right:20px;\"><h2>" . $record['Title'] . "</h2>" . $record['Ref'] . "<br/>" . "£" . $record['Price'] . "<br/>" . $record['Location'] . "<br/>" . "Details</div>" . "<img src=\"images/bottom-line.png\" id=\"bottomline\"></div>" . "</div>" ;
}
mysql_close($con);
?>
Any help would be greatly received. Thanks in advance!
I would suggest using the same page for displaying the results, whether the filter applies to the location or the number of bedrooms. You can always use WHERE (1 = 1) in your SQL so that you can append AND (Location='$loc') or AND (Beds='$bed'), though it would be preferable to use parameters.
Once you have that set up, you can modify your links. Let's say your destination page is webproperty.php. Store that URL in a variable $urlDest.
When you generate your links for filtering by location, add any variables in $_GET that aren't loc to the end of $urlDest, getting something like webproperty.php?bed=2. As you iterate through your locations, append the loc variable to $urlDest, like "<a href=\"$urlDest&loc=$loc\">".
Similarly, when you generate your links for filtering by bedroom count, add any variables in $_GET that aren't bed to the end of $urlDest, getting something like webproperty.php?loc=Alsancak. As you iterate through your bedroom counts, append the bed variable to $urlDest, like "<a href=\"$urlDest&bed=$bed\">".

PHP while loop multiple conditions returning no data?

I have a PHP page that is returning data from a MySQL table. I decided to add a feature that would allow the user to set the number of results they want displayed, in case there are thousands of records for example.
Here is the PHP code that displays the data:
while ($row = mysql_fetch_array($result) && ($row <= $noOfResults)) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
I was hoping that this would only display the data up to the point that the user has selected. So if the user selects 10 results, $noOfResults is set to 10, and it will only fetch the first 10 results from the database. This is currently however only displaying a "-" and the $noOfResults variable (which is desired at this point). Is my syntax wrong or is this not the correct method of going about such a problem?
Thanks, got it working with LIMIT, didn't even think to do that.
Can someone explain why this was downvoted, just trying to learn, so as to write better questions in the future, thanks.
the best way to get limited data from database is LIMIT statement in query .
i assume that your query is
$result= "SELECT * FROM `mytable`";
Just add limit statement in
$result= "SELECT * FROM `mytable` LIMIT '$noOfResults'";
then your while loop will be
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
You are doing it wrong $row will be an array. So to correct this use mysql_num_rows() function in php to check the number of the rows. Try the code below.
if(mysql_num_rows($result)<=$noOfResults){
while ($row = mysql_fetch_array($result) ) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo '<hr align="center" width="100%" />';
echo $noOfResults;
}
}
Hope this helps you

get id from url in same page

I am using the toggle visibility function in order to display and hide divs in one page.
Clicking on this link News makes a div visible where all the current news are displayed from the database using this code.
<?php
include"scripts/connect_to_mysql.php";
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$news="";
$sql=mysql_query("SELECT *
FROM `news`
ORDER BY date DESC");
$newsCount=mysql_num_rows($sql);
if ($newsCount>0) {
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$title=$row["title"];
$text=$row["text"];
$date=$row["date"];
$news.=' <table width="800" border="0">
<tr>
<td style="width:150px;">' . $date . '</td>
<td style="width:600px; overflow:hidden;"><a href="#?id=' . $id . '" onclick="toggle_visibility(\'news_det\');" style="color:#b19057;" >' . $title . '</a></td>
<td style="width:50px">...more</td>
</tr>
</table>
';
}
}else {
$news="No news available yet";
}
?>
The problem is that by clicking on the results, the div where the detailed news should appear, appears, but I can't get the data from the database.
I use this code the get the id sent from the previous links.
if(isset($_GET['id'])){
$newsid= preg_replace('#[^0-9]#i','',$_GET['id']);
$sql = mysql_query("SELECT * FROM news WHERE id='$newsid' LIMIT 1");
$newsCount1 = mysql_num_rows($sql);
if ($newsCount1 > 0) {
while($row = mysql_fetch_array($sql)){
$dettitle = $row["title"];
$dettext = $row["text"];
$detdate = $row["date"];
}
}
else {
echo "No news with that id";
exit();
}
}
What am I doing wrong?
How to get the variable with PHP?
Let's say you have a PHP page named people.php. Now you can call this page using the following URL:
people.php?name=Joe
With PHP, you will be able to get the value of the variable 'name' like this:
$_GET["name"]
So, you use documentation $_GET to find the value of a named variable. Let's try it in an example:
<html>
<head>
<title>Query string</title>
</head>
<body>
<?php
// The value of the variable name is found
echo "<h1>Hello " . $_GET["name"] . "</h1>";
?>
</body>
</html>
May be its because you are using '#' in link
Change href="#?id=' . $id . '" to href="?id=' . $id . '"
If you are not refreshing the page then you should use ajax for this.

PHP returned code not showing up, but it used to

I run a website for my church. In a database on the server, we store all of the newest sermons' information so that when a user clicks a button on the first page, the newest sermons will show up in a div. For a long time, everything was working just great, but now, the returned code just doesn't show up.
The weird thing is, if I open the page in Google Chrome and hit "Inspect Element" over the div, the returned code just appears. It looks perfect... After I have done that, clicking on the button will again load the info from the database and it will show up; if I refresh the page, however, the returned stuff goes away again until I inspect the element.
Try it for yourself here. Click the orange button labeled "New Message."
NOTE:
The div that pops up is id navNew
The background transparent div that pops up is id navBackground
Within the div id navNew, I have a p id navNewBody that holds the information returned by the get PHP page below.
My code for the PHP getter page:
<?php
$link = mysql_connect('domain', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(flf);
$r = mysql_query("SELECT * FROM new");
$first = mysql_num_rows($r);
$last = $first - 20;
echo '<p class="NavHead">New Sermons<br/><table width="90%" border="0" cellpadding="0" cellspacing="0">';
$ind = 0;
while($first > $last){
$me=mysql_fetch_array(mysql_query("SELECT * FROM new WHERE id='" . $first . "'"));
$name = $me["name"];
$row = 21-$ind;
echo '<tr id="row' . $row . '" class="navClosed" onclick="expand(' . "'" . 'row' . $row . "'" . ',' . $row . ')"><td colspan="4"> ' . $name . '</td></tr><tr id="row' . $row . 'e" style="visibility: hidden" class="navOpenClosed" onclick="dismiss(' . "'" . 'row' . $row . "'" . ')"></tr>';
$first = $first - 1;
$ind = $ind + 1;
}
echo "</table></p>";
?>
And here is the code I use to access the page (yes I imported jQuery):
function retNew(){
document.getElementById("navNewBody").innerHTML = '<span class="NavHead"><p align="center">Loading...Please Wait...<br/><br/><br/><img src="Sermons/Style/Loading.gif" /></p></span>';
document.getElementById("navNew").style.visibility = "visible";
$("#navNewBody").load("retNew.php");
}
What's wrong? It just doesn't make sense to me. I set the z-index to 10,000,000 and nothing happened. Any ideas? Thanks in advance.
I think you visibility is messing with the content. Try to use this load.
function retNew(){
$('#navNewBody').html('<div class="NavHead"><p align="center">Text</p></div>')
.load('retNew.php');
$('#navNew').fadeIn();
}
As of the close button, in your case it should be something like that:
<div id="navNew">
<a class="LargeNameU" onclick="offNavNew();return false;"
style='position:fixed; z-index: 1000; right:0px;top:0px'>
<img src="Sermons/Style/x.png" border='0'>
</a>
<p align="center" style="z-index:10000000" id="navNewBody">
and so on. Your .navNew already has a fixed position.
The visibility property has some very weird problems. I would recommend using display instead. The only difference between the two is that display removes the element entirely when it's set to none, while visibility just essentially makes the element invisible (but still leaves a space in the page where the element was).

Categories