Ok, so I'm trying to make some simple code to display news articles from a MySQL server but all I get is a completely blank middle part of the page where the news articles are supposed to be. Here is the code:
<?
$query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<div class=\"newsItem\">";
echo "<h2>" . $row['header'] . "</h2>";
echo "<p>" . $row['content'] . "</p>";
echo "</div>";
}
?>
The problem seems to be with the while loop. If I write echo "WTF"; outside the loop it will show but if i write it inside it wont show. I'm not really good at PHP so I'm puzzled. ID is INT and Primary Key, header is VARCHAR(255) and content is TEXT. Any Ideas? Also the connect scrips works cuz I dont get error messages when it dies.
Try adding an error catch:
$query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
$result = mysql_query($query) or die(mysql_error());
OR you have no results. so add somthing for that:
if(mysql_num_row($result) > 0){
while($row = mysql_fetch_array($result))
{
echo "<div class=\"newsItem\">";
echo "<h2>" . $row['header'] . "</h2>";
echo "<p>" . $row['content'] . "</p>";
echo "</div>";
}
}
else {echo 'no results';}
You either have no records in your news table or displaying warnings isn't enabled (slap)
There aren't any news in news table
One or more columns are missing
Table news does not exists
in your case, try to replace your 2nd line with
$query = "SELECT ID, content FROM news ORDER BY ID DESC";
You may have a database rights issue or your query may have an error but the error isn't being displayed.
If you don't have display_errors turned on in your php.ini, you should take a look in the web server error log file to see if an error is being logged when you connect to the MySql database.
You could also try:
ini_set('display_errors', 1);
At the top of your script which will force any database connection or query errors to be displayed in the resulting web page.
NOTE: This is a feature to support your development and should never be used on production systems.
Related
//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table output:
This code will select data. Additionally, there is reference to order.php on every line. When the user clicks on reference( <a href> clause), it opens order.php and there I need to know which row the user selected to work with these data.
Change the code to:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
In order.php you can then access these values by using the $_GET["city"] and $_GET["country"] variables which contain the values from your <a href> link on the previous page. For example, running echo $_GET["city"]; will output the city name.
Edit: As #Rizier123 pointed out, using a unique ID might be more prone to errors in case your database contains more than one entry for the same city or country. You should consider introducing an ID in your table structure and then using that in the link to order.php.
I have multiple links on a page where each link is suppose to return a specific row of data from a database. When the link is clicked, the user is forwarded to another page where the info associated with that link is displayed. Here is the code:
//db connection: (using xampp)
mysql_connect('localhost', 'root', '');
mysql_select_db('db_name');
$sql = "SELECT * FROM user_input";
$records = mysql_query($sql);
//code:
<div>
$open_report = mtsql_fetch_assoc($records);
echo "Error Report# {$open_report['id']};
echo "<p>" .$open_report['comments'] . "</p>";
</div>
The problem is it always returns the same row of data. Each row in the db is associated with a link and when that link is clicked I want to return the associated row of data in the db. I think it may have to do with this line: $sql = "SELECT * FROM user_input"; but I'm not sure how to fix it. If anyone can help it would be greatly appreciated.
I have restructured my answer to give it a better flow. I also noticed you are using mysql_ not mysqli_ . You need to use mysqli_ as mysql is depreciated.
EDIT: This would be the page that displays all the error reports. You would want to output them in the form of a hyperlink that passes a GET parameter to the page that shows the details.
$sql = "SELECT ID, Description, etc, etc from reports";
$open_reports = mysqli_query($sql);
//error check here as well if ANY results were returned
while($row = mysqli_fetch_array($open_reports, MYSQLI_ASSOC)) {
echo ''' . $open_reports['Description'] . '';
}
This will give you links that look like
detailspage.php?id=1 detailspage.php?id=2
etc...
On the "detailspage.php" You can capture that ID and display dynamic information on that same page.
if (isset($_GET['ID'])){
$sql = "Select * from user_input where ID='" . $_GET['id'] . "'";
$records = mysqli_query($sql)
while($open_report = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
echo "Error Report# " . $open_report['id'] . "<br/>";
echo "<p>" .$open_report['comments'] . "</p>";
}
}
I know the basics of MySQL and PHP. I've set up a posting system that echoes the fields from my table.
The only problem is, since this is a news page, the newest posts need to be on top. And when the page echoes back the results, it puts the first entry I made (in the table) at the top.
This is my code:
// Selecting The Table
$result = mysql_query("SELECT * FROM posts")
or die(mysql_error());
// Connecting To The Rows, And Echoing Back The Results
while($row = mysql_fetch_array($result)) {
echo "<div class='post'>";
echo "Title: " . $row['title'];
echo "<br />";
echo $row['content'];
echo "</div>";
I am not sure how to make the latest post the first to show up.
I still want it to echo back the older entries though.
I can use phpMyAdmin if necessary.
You need to order your results.
SELECT * FROM posts ORDER BY date_posted DESC
Ok, so. I have asked about 5 questions on stackOverflow today, you've all been so helpful.
Now, i'm a designer, learning to code, so bear with me.
I have a mySQL table, with a small CMS/Blog im building.
I have it styled how I want to now.
This is the code for the page.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<h1>" . $row['Title'] . "</h1>";
echo "<br>";
echo "<h2>" . "Posted on: " . $row['Date'] . "</h2>";
echo "<br>";
echo "<img src='".$row['Image']."'/>";
echo "<br>";
echo $row['Message'];
echo "<br>";
echo "<br>";
}
I'm still working on it so its all good.
What I want to know is, this code is outputting my sql data into a page. Is there any way of telling the page what order to echo the data, for instance. in my SQL table i have:
2012-11-03 16:16:06 This is my First Blog Post This is the first message of the first blog post. ... http://blog.gettyimages.com/wp-content/uploads/201.
and next is
2012-11-03 16:17:29 This is my Second Blog Post This is the second message of the Second Post, You... http://www.aviation-images.com/user/zooms/118/451n...
How can i tell the page to Always display the most recent post at the top, and older ones below.?
Use order by in your query:
$result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
If your MySQL database schema uses DATETIME for the Date column, you can simply sort in the MySQL query with ORDER BY:
$result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
If you have an auto-increment column like post_id, then you can use ORDER BY post_id DESC as well. :)
I've been trying for hours to figure out how to put a link into the following text output via PHP echo(). I basically want to make the title field I'm pulling from my events table (as seen in #4 in the code below) to come back into the browser as a link instead of just text...
the original code that brings back the event title:
<?php
// 3. Perform database Query to bring list of events
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
// 4. Use returned Data
while ($row = mysql_fetch_array($result)) {
echo $row ["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
How would I go about getting that $row["eventtitle"] to appear in the browser as a link? Let's say if the link was just "eventprofile.php". This is probably an easy fix, but I've been getting a million errors with trying different things with <a href>s.
<?php
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo "".$row["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
add an anchor tag for it!!
echo "" . $row['eventtitle'] . '' . "<br />" .$row["eventdesc"]."<br/>";
And thats it.